import re
from pathlib import Path

root = Path(__file__).resolve().parents[1]
html = (root / "index.html").read_text(encoding="utf-8")
m = re.search(r"<main>(.*)</main>", html, re.S)
if not m:
    raise SystemExit("No <main> in index.html")
text = m.group(1)

# Drop hero — replaced by includes/home-hero.php (dynamic banners)
text = re.sub(
    r"\s*<!-- hero-area start -->.*?<!-- hero-area end -->\s*",
    "\n",
    text,
    flags=re.S,
)

text = re.sub(
    r'(href|src|data-background|data-mask-src)="(assets/[^"]+)"',
    lambda m: f'{m.group(1)}="<?= asset(\'{m.group(2)}\') ?>"',
    text,
)

repl_links = [
    ("href=\"index.html\"", 'href="index.php"'),
    ("href=\"contact.html\"", 'href="contact.php"'),
    ("href=\"about.html\"", 'href="about.php"'),
    ("href=\"service.html\"", 'href="services.php"'),
    ("href=\"service-details.html\"", 'href="services.php"'),
    ("href=\"project.html\"", 'href="portfolio.php"'),
    ("href=\"project-details.html\"", 'href="portfolio.php"'),
    ("href=\"casestudy.html\"", 'href="portfolio.php"'),
    ("href=\"pricing.html\"", 'href="contact.php"'),
    ("href=\"blog-standard.html\"", 'href="#"'),
    ("href=\"blog-post.html\"", 'href="#"'),
    ("href=\"blog-details.html\"", 'href="#"'),
    ("href=\"faq.html\"", 'href="faq.php"'),
    ("href=\"team.html\"", 'href="teams.php"'),
    ("href=\"team-details.html\"", 'href="teams.php"'),
]
for a, b in repl_links:
    text = text.replace(a, b)

out = root / "includes" / "home-main-static.php"
out.write_text(
    "<?php\n"
    "/** Static home sections from index.html (hero is includes/home-hero.php). */\n"
    "declare(strict_types=1);\n"
    "?>\n" + text,
    encoding="utf-8",
)
print("Wrote", out.name, out.stat().st_size)
