#!/usr/bin/env python3
"""Verify that a generated AI流程再造方案 keeps the golden visual system."""

from __future__ import annotations

import argparse
import re
import sys
from pathlib import Path


REQUIRED_SNIPPETS = [
    "@page",
    "size: A4",
    "width: 210mm",
    "min-height: 297mm",
    "padding: 14mm 15mm 13mm",
    "page-break-after: always",
    "--ink: #1f2933",
    "--muted: #60707c",
    "--line: #d9e1e8",
    "--teal: #0f766e",
    "--amber: #c7781c",
    "--coral: #d55745",
    "--green: #2f7d4f",
    "--blue: #365f8f",
    "--dark: #24313a",
    "--cream: #fbfaf6",
    ".page.dark",
    ".kicker",
    ".statement",
    ".grid-3",
    ".grid-2",
    ".card",
    ".flow",
    ".two-track",
    ".matrix",
    ".roadmap",
    ".question-list",
    ".author-wrap",
    ".author-intro",
    ".qr-card",
]

REQUIRED_BODY_SNIPPETS = [
    'class="page dark"',
    'class="kicker"',
    'class="statement"',
    'class="card',
    'class="flow"',
    'class="matrix"',
    'class="roadmap"',
    'class="question-list"',
    'class="author-wrap"',
    'class="qr-card"',
    "詹老师",
    "13136092523",
    "zhansheng_talk_qr_cropped.jpg",
]


def extract_style(html: str) -> str:
    match = re.search(r"<style>(.*?)</style>", html, flags=re.S)
    if not match:
        return ""
    return re.sub(r"\s+", " ", match.group(1)).strip()


def count_pages(html: str) -> int:
    return len(re.findall(r'<section class="page(?: dark)?"', html))


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("html", help="Generated report HTML")
    parser.add_argument("--golden", required=True, help="Golden sample HTML")
    parser.add_argument("--allow-css-drift", action="store_true", help="Warn instead of fail when CSS differs")
    args = parser.parse_args()

    target_path = Path(args.html)
    golden_path = Path(args.golden)
    target = target_path.read_text(encoding="utf-8")
    golden = golden_path.read_text(encoding="utf-8")

    failures: list[str] = []
    warnings: list[str] = []

    target_style = extract_style(target)
    golden_style = extract_style(golden)

    if not target_style:
        failures.append("No <style> block found.")
    elif target_style != golden_style:
        message = "CSS differs from golden_sample.html; visual 1:1 lock is broken."
        if args.allow_css_drift:
            warnings.append(message)
        else:
            failures.append(message)

    for snippet in REQUIRED_SNIPPETS:
        if snippet not in target:
            failures.append(f"Missing visual snippet: {snippet}")

    for snippet in REQUIRED_BODY_SNIPPETS:
        if snippet not in target:
            failures.append(f"Missing body snippet: {snippet}")

    pages = count_pages(target)
    if not 12 <= pages <= 15:
        failures.append(f"Expected 12-15 pages, found {pages}.")

    if "About Author" not in target or "关于作者" not in target:
        failures.append("Missing fixed author page title.")

    if failures:
        print("VISUAL LOCK FAILED")
        for item in failures:
            print(f"- {item}")
        for item in warnings:
            print(f"WARNING: {item}")
        return 1

    print("VISUAL LOCK PASSED")
    print(f"pages: {pages}")
    for item in warnings:
        print(f"WARNING: {item}")
    return 0


if __name__ == "__main__":
    sys.exit(main())
