#!/usr/bin/env python3
"""Extract body content from raw product HTML files into clean snippets.

Strips <html>/<head>/<body> wrappers, removes <style>/<script>/<link>/<meta>
tags, and writes into site/content/product-details/{slug}.html.
"""

from __future__ import annotations

import re
from pathlib import Path

HERE = Path(__file__).resolve().parent
SITE = HERE.parent
ROOT = SITE.parent
OUT_DIR = SITE / "content" / "product-details"

SOURCES = {
    "consentiq": ROOT / "ConsentIQ_by_OrthoGlobe (4).html",
    "atlas": ROOT / "ortho-atlas (1).html",
    "orthoprep": ROOT / "OrthoPrep_1300_Platform (2).html",
    "academy": ROOT / "orthoglobe-courses.html",
}


def extract_body(html: str) -> str:
    # Strip <style>, <script>, <noscript>, <link>, <meta> BEFORE trying to
    # find the body — some sources embed stray </body> strings inside inline
    # script template literals which confuses body matching.
    cleaned = re.sub(
        r"<style\b[^>]*>.*?</style>",
        "",
        html,
        flags=re.IGNORECASE | re.DOTALL,
    )
    cleaned = re.sub(
        r"<script\b[^>]*>[\s\S]*?</script>",
        "",
        cleaned,
        flags=re.IGNORECASE,
    )
    # Handle unterminated scripts by stripping from <script ...> to next
    # recognisable top-level tag boundary — safest to drop everything from
    # an unterminated <script> to </body>.
    if re.search(r"<script\b", cleaned, re.IGNORECASE):
        cleaned = re.sub(
            r"<script\b[^>]*>[\s\S]*?(?=</body>|$)",
            "",
            cleaned,
            flags=re.IGNORECASE,
        )
    cleaned = re.sub(
        r"<noscript\b[^>]*>.*?</noscript>",
        "",
        cleaned,
        flags=re.IGNORECASE | re.DOTALL,
    )
    cleaned = re.sub(r"<link\b[^>]*/?>", "", cleaned, flags=re.IGNORECASE)
    cleaned = re.sub(r"<meta\b[^>]*/?>", "", cleaned, flags=re.IGNORECASE)

    m = re.search(r"<body[^>]*>([\s\S]*?)</body>", cleaned, re.IGNORECASE)
    if m:
        inner = m.group(1)
    else:
        inner = cleaned

    # Collapse excessive blank lines
    inner = re.sub(r"\n\s*\n\s*\n+", "\n\n", inner)
    return inner.strip() + "\n"


def main():
    OUT_DIR.mkdir(parents=True, exist_ok=True)
    for slug, src in SOURCES.items():
        if not src.exists():
            print(f"MISSING source: {src}")
            continue
        raw = src.read_text(encoding="utf-8", errors="replace")
        body = extract_body(raw)
        out = OUT_DIR / f"{slug}.html"
        out.write_text(body, encoding="utf-8")
        print(f"{slug}: {len(body)} chars  ->  {out}")


if __name__ == "__main__":
    main()
