import os
import re
from pathlib import Path
from urllib.parse import urlparse
from typing import Optional

ROOT = Path(__file__).resolve().parents[1]

PAGE_EXTS = {".php", ".html", ".htm"}
HREF_RE = re.compile(r'href\s*=\s*(?:"([^"]+)"|\'([^\']+)\')', re.IGNORECASE)


def is_external(href: str) -> bool:
    if href.startswith(("mailto:", "tel:", "javascript:", "#", "data:")):
        return True
    u = urlparse(href)
    return u.scheme in {"http", "https"} or href.startswith("//")


def map_to_path(from_file: Path, href: str) -> Optional[Path]:
    href = href.split("#", 1)[0].split("?", 1)[0].strip()
    if not href or is_external(href):
        return None

    if href.startswith("/aaa/"):
        return ROOT / href.removeprefix("/aaa/")
    if href.startswith("/"):
        return ROOT / href.lstrip("/")
    return (from_file.parent / href).resolve()


def main():
    missing = []
    checked = 0

    for dirpath, dirnames, filenames in os.walk(ROOT):
        dirnames[:] = [d for d in dirnames if d not in {"_next", "__website-next", "cdn-cgi", "node_modules"}]
        for fn in filenames:
            p = Path(dirpath) / fn
            if p.suffix.lower() not in PAGE_EXTS:
                continue
            # keep scan fast: skip very large pages
            try:
                if p.stat().st_size > 120_000:
                    continue
            except Exception:
                pass
            try:
                data = p.read_text(encoding="utf-8", errors="ignore")
            except Exception:
                continue

            for m in HREF_RE.finditer(data):
                href = (m.group(1) or m.group(2) or "").strip()
                if not href or is_external(href):
                    continue
                # only check page-like targets
                if not (href.endswith(".php") or href.endswith(".html") or href.endswith(".htm") or href.endswith("/")):
                    continue
                target = map_to_path(p, href)
                if target is None:
                    continue
                try:
                    target.relative_to(ROOT)
                except Exception:
                    continue
                checked += 1
                if href.endswith("/") and target.exists() and target.is_dir():
                    continue
                if not target.exists():
                    missing.append((str(p.relative_to(ROOT)), href))

    out = ROOT / "tools" / "broken_internal_page_links.txt"
    out.write_text("\n".join([f"{src} -> {href}" for src, href in missing]) + ("\n" if missing else ""), encoding="utf-8")
    print(f"Checked links: {checked}")
    print(f"Broken page links: {len(missing)}")
    print(f"Wrote: {out}")


if __name__ == "__main__":
    main()

