"""
Fix page.tsx - correctly extract the FULL body HTML from the big dangerouslySetInnerHTML block
"""

filepath = r'd:\Ai\AntiGravity\Project_1.0\gmap\leadforge\src\app\page.tsx'

with open(filepath, 'r', encoding='utf-8') as f:
    content = f.read()

# Find the big body block - it starts with the page-wrapper div
# Look for `\n    <div class="page-wrapper">  ... up to ` }} />
start_marker = '<div class="page-wrapper">'
end_marker = '` }} />'

start_idx = content.find(start_marker)
if start_idx == -1:
    print("ERROR: Could not find page-wrapper div")
    print("Searching for alternative markers...")
    # Try to find any large block
    all_backtick_starts = [i for i in range(len(content)) if content[i] == '`']
    for idx in all_backtick_starts:
        snippet = content[idx+1:idx+100]
        print(f"Backtick at {idx}: {repr(snippet[:80])}")
    exit(1)

# Find the end - the ` }} /> after the body block
# We need to find the right closing backtick (the one after the huge body block)
end_idx = content.find(end_marker, start_idx)
if end_idx == -1:
    print("ERROR: Could not find closing backtick marker")
    exit(1)

body_html = content[start_idx:end_idx]
print(f"Body HTML extracted: {len(body_html)} chars")
print(f"First 100 chars: {body_html[:100]}")
print(f"Last 100 chars: {body_html[-100:]}")

# Verify it has enough content
if len(body_html) < 10000:
    print("WARNING: Body seems too small - something may be wrong")

# Build the proper new server component
new_content = f'''import Script from "next/script";
import type {{ Metadata }} from "next";

export const metadata: Metadata = {{
  title: "LeadForge - AI-Powered Lead Generation",
  description: "LeadForge transforms complex data into clear, actionable insights — empowering teams to track performance, optimize strategies, and forecast growth with ease.",
}};

export default function LandingPage() {{
  return (
    <>
      {{/* Webflow stylesheet — Next.js hoists <link> to <head> in server components */}}
      <link
        href="https://cdn.prod.website-files.com/69059456676850507afd94d2/css/leadforge.webflow.shared.9c3670485.css"
        rel="stylesheet"
        type="text/css"
      />
      <link href="https://fonts.googleapis.com" rel="preconnect" />
      <link href="https://fonts.gstatic.com" rel="preconnect" crossOrigin="anonymous" />

      {{/* Webflow visibility init style */}}
      <style dangerouslySetInnerHTML={{{{ __html: `
        html.w-mod-js:not(.w-mod-ix3) :is(.about-title) {{
          visibility: hidden !important;
        }}
      ` }}}} />

      {{/* Main page body injected from Webflow export */}}
      <div dangerouslySetInnerHTML={{{{ __html: `{body_html}` }}}} />

      {{/* Webflow JS — runs after page load */}}
      <Script
        src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js"
        strategy="afterInteractive"
      />
      <Script id="wf-webfont" strategy="afterInteractive">{{`
        WebFont.load({{
          google: {{ families: ["Plus Jakarta Sans:300,regular,500,600,700,800", "Inter Tight:regular,800"] }}
        }});
      `}}</Script>
      <Script id="wf-touch" strategy="afterInteractive">{{`
        !function(o,c){{var n=c.documentElement,t=" w-mod-";n.className+=t+"js",("ontouchstart"in o||o.DocumentTouch&&c instanceof DocumentTouch)&&(n.className+=t+"touch")}}(window,document);
      `}}</Script>
      <Script
        src="https://d3e54v103j8qbb.cloudfront.net/js/jquery-3.5.1.min.dc5e7f18c8.js?site=69059456676850507afd94d2"
        strategy="afterInteractive"
      />
      <Script
        src="https://cdn.prod.website-files.com/69059456676850507afd94d2/js/webflow.schunk.e0c428ff9737f919.js"
        strategy="afterInteractive"
      />
      <Script
        src="https://cdn.prod.website-files.com/69059456676850507afd94d2/js/webflow.schunk.804ea3a89620ea55.js"
        strategy="afterInteractive"
      />
      <Script
        src="https://cdn.prod.website-files.com/69059456676850507afd94d2/js/webflow.bb5eeaea.085e76713d135d5c.js"
        strategy="afterInteractive"
      />
      <Script src="https://cdn.prod.website-files.com/gsap/3.14.2/gsap.min.js" strategy="afterInteractive" />
      <Script src="https://cdn.prod.website-files.com/gsap/3.14.2/SplitText.min.js" strategy="afterInteractive" />
      <Script src="https://cdn.prod.website-files.com/gsap/3.14.2/ScrollTrigger.min.js" strategy="afterInteractive" />
    </>
  );
}}
'''

with open(filepath, 'w', encoding='utf-8') as f:
    f.write(new_content)

lines = new_content.count('\n')
print(f"Done. New file: {len(new_content)} bytes, {lines} lines")
