"""
Fix page.tsx:
1. Remove "use client" 
2. Remove the <head> wrapper (Next.js will auto-hoist <link> tags in server components)
3. Fix the Script tags at the bottom (they're all on one line as a string)
4. Keep body dangerouslySetInnerHTML intact
"""

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()

# Read the body-only section (lines 34 to 2007 = the dangerouslySetInnerHTML block)
# We'll rebuild the file from scratch with the correct structure

# Extract just the HTML body content from the dangerouslySetInnerHTML
import re

# Find the body content between the backticks in the big dangerouslySetInnerHTML block
body_match = re.search(r'\{ __html: `(.*?)` \}\}', content, re.DOTALL)
if not body_match:
    print("ERROR: Could not find body content")
    exit(1)

body_html = body_match.group(1)
print(f"Body HTML extracted: {len(body_html)} chars")
print(f"First 100 chars: {body_html[:100]}")

# Build the new page.tsx as a proper server component
new_content = '''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 inline init style */}
      <style dangerouslySetInnerHTML={{ __html: `
        html.w-mod-js:not(.w-mod-ix3) :is(.about-title) {
          visibility: hidden !important;
        }
      ` }} />

      {/* Page body injected from Webflow export */}
      <div dangerouslySetInnerHTML={{ __html: BODY_HTML_PLACEHOLDER }} />

      {/* Webflow JS — runs after page load */}
      <Script
        src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js"
        strategy="afterInteractive"
      />
      <Script id="webfont-load" 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"
      />
    </>
  );
}
'''

# Insert the actual body HTML
new_content = new_content.replace('BODY_HTML_PLACEHOLDER', '`' + body_html + '`')

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

print(f"Done. New file size: {len(new_content)} bytes, {new_content.count(chr(10))} lines")
