#!/usr/bin/env python3
"""
E2E Test Suite Runner for ANL Homepage Development
Verifies:
0. Clean step (npm run clean -> removes .next and tsconfig.tsbuildinfo)
1. ESLint (npm run lint)
2. Next.js Static SSG Build (npm run build) with 0 dynamic routes
3. TypeScript type check (npx tsc --noEmit)
4. Existence and content of static SSG routes HTML/body files
5. Data count losslessness & integrity gate (verify_counts.py)
6. Light/Dark mode theme tokens and CSS attributes in source and build assets
7. DOM & HTML Content Assertions (Unittest)
"""

import os
import sys
import subprocess
import re

ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
REFER_DIR = os.path.join(ROOT_DIR, "refer_landing_page")

# Expected Static SSG Routes HTML/Body output paths relative to REFER_DIR
EXPECTED_SSG_ROUTES = [
    os.path.join(".next", "server", "app", "index.html"),
    os.path.join(".next", "server", "app", "members.html"),
    os.path.join(".next", "server", "app", "publications.html"),
    os.path.join(".next", "server", "app", "publications", "intl-journal-conf.html"),
    os.path.join(".next", "server", "app", "publications", "domestic-journal-conf.html"),
    os.path.join(".next", "server", "app", "publications", "patent.html"),
    os.path.join(".next", "server", "app", "lectures.html"),
    os.path.join(".next", "server", "app", "standardization.html"),
    os.path.join(".next", "server", "app", "_not-found.html"),
    os.path.join(".next", "server", "app", "robots.txt.body"),
    os.path.join(".next", "server", "app", "sitemap.xml.body"),
    os.path.join(".next", "server", "pages", "404.html"),
]

def run_cmd(cmd, cwd=REFER_DIR):
    print(f"Running: {' '.join(cmd)} (cwd: {cwd})")
    res = subprocess.run(cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    return res

def test_clean():
    print("\n--- Gate 0: Clean Step (workspace build cache purge) ---")
    res = run_cmd(["bash", "-c", "npm run clean"])
    if res.returncode != 0:
        print(f"❌ Clean step failed:\n{res.stdout}\n{res.stderr}")
        return False
    print("✅ Gate 0 PASSED: Cleaned .next and tsconfig.tsbuildinfo")
    return True

def test_lint():
    print("\n--- Gate 1: ESLint Quality Gate ---")
    res = run_cmd(["npm", "run", "lint"])
    if res.returncode != 0:
        print(f"❌ Lint failed:\n{res.stdout}\n{res.stderr}")
        return False
    print("✅ Gate 1 PASSED: ESLint clean (0 errors, 0 warnings)")
    return True

def test_build():
    print("\n--- Gate 2: Next.js Static SSG Build Gate ---")
    res = run_cmd(["npm", "run", "build"])
    if res.returncode != 0:
        print(f"❌ SSG Build failed:\n{res.stdout}\n{res.stderr}")
        return False
    
    # Check stdout/stderr for dynamic routes (f Dynamic)
    if "ƒ" in res.stdout or "Dynamic" in res.stdout:
        # Search specifically for dynamic routes output line
        lines = res.stdout.splitlines()
        dynamic_lines = [l for l in lines if "ƒ" in l or "Dynamic" in l]
        if dynamic_lines:
            print(f"❌ Found dynamic route(s) in build report: {dynamic_lines}")
            return False

    print("✅ Gate 2 PASSED: Static SSG Build successful (0 dynamic routes)")
    return True

def test_tsc():
    print("\n--- Gate 3: TypeScript Type Safety Gate ---")
    res = run_cmd(["npx", "tsc", "--noEmit"])
    if res.returncode != 0:
        print(f"❌ Type check failed:\n{res.stdout}\n{res.stderr}")
        return False
    print("✅ Gate 3 PASSED: TypeScript type check clean (exit code 0)")
    return True

def test_ssg_routes():
    print("\n--- Gate 4: Static SSG HTML Route Verification ---")
    missing = []
    for rel_path in EXPECTED_SSG_ROUTES:
        full_path = os.path.join(REFER_DIR, rel_path)
        if not os.path.exists(full_path):
            missing.append(rel_path)
        else:
            size = os.path.getsize(full_path)
            print(f"  [OK] {rel_path} ({size} bytes)")

    if missing:
        print(f"❌ Missing {len(missing)} SSG routes:")
        for m in missing:
            print(f"  - {m}")
        return False

    print(f"✅ Gate 4 PASSED: All {len(EXPECTED_SSG_ROUTES)} static SSG routes exist and are non-empty")
    return True

def test_data_counts():
    print("\n--- Gate 5: Data Losslessness Integrity Gate ---")
    script_path = os.path.join(REFER_DIR, "scripts", "extract", "verify_counts.py")
    res = run_cmd([sys.executable, script_path], cwd=ROOT_DIR)
    if res.returncode != 0:
        print(f"❌ Data count verification failed:\n{res.stdout}\n{res.stderr}")
        return False
    print(res.stdout.strip())
    print("✅ Gate 5 PASSED: Data count integrity & volume losslessness verified")
    return True

def test_theme_tokens():
    print("\n--- Gate 6: CSS & Theme Token Verification ---")
    globals_css = os.path.join(REFER_DIR, "app", "globals.css")
    tailwind_config = os.path.join(REFER_DIR, "tailwind.config.ts")
    
    if not os.path.exists(globals_css):
        print(f"❌ Missing {globals_css}")
        return False
    
    with open(globals_css, "r", encoding="utf-8") as f:
        css_text = f.read()

    tokens = ["--ink", "--ivory", "--paper", "--line", "--vermillion", "--cobalt"]
    missing_tokens = [t for t in tokens if t not in css_text]
    if missing_tokens:
        print(f"❌ Missing CSS theme tokens in globals.css: {missing_tokens}")
        return False

    with open(tailwind_config, "r", encoding="utf-8") as f:
        tw_text = f.read()

    tw_tokens = ["vermillion", "cobalt", "dark"]
    missing_tw = [t for t in tw_tokens if t not in tw_text]
    if missing_tw:
        print(f"❌ Missing Tailwind theme tokens in tailwind.config.ts: {missing_tw}")
        return False

    print("✅ Gate 6 PASSED: Light/Dark theme CSS variables and Tailwind tokens present")
    return True

def test_unittest_suite():
    print("\n--- Gate 7: DOM & HTML Content Assertions (Unittest) ---")
    test_path = os.path.join(REFER_DIR, "tests", "e2e_test_suite.py")
    res = run_cmd([sys.executable, "-m", "unittest", test_path], cwd=ROOT_DIR)
    if res.returncode != 0:
        print(f"❌ Unittest suite failed:\n{res.stdout}\n{res.stderr}")
        return False
    print("✅ Gate 7 PASSED: All 7 DOM & static HTML content assertion tests passed")
    return True

def test_layout_architecture():
    print("\n--- Gate 8: Layout Architecture & No-Escape Gate (AC-25 / AC-26 / AC-40 / AC-41 / R-8.17) ---")
    
    # 1) Check HTML SSG pages for container-content inside main across ALL SSG routes (C2, AC-25)
    routes_to_check = EXPECTED_SSG_ROUTES
    
    main_hashes = {}
    for rel_p in routes_to_check:
        if not rel_p.endswith(".html"):
            continue
        full_p = os.path.join(REFER_DIR, rel_p)
        if not os.path.exists(full_p):
            print(f"❌ Missing SSG route HTML: {rel_p}")
            return False
        with open(full_p, "r", encoding="utf-8") as f:
            html = f.read()
        
        # Verify <main> tag exists and does NOT contain max-w- or px-
        main_match = re.search(r'<main(?:\s+[^>]*)?>(.*?)</main>', html, flags=re.S)
        if not main_match:
            print(f"❌ <main> tag not found in {rel_p}")
            return False
        
        main_inner = main_match.group(1)
        
        # C1: Verify child wrapper inside <main> has container-content
        if "container-content" not in main_inner and "_not-found" not in rel_p and "404" not in rel_p:
            print(f"❌ container-content missing inside <main> in {rel_p}")
            return False

        # AC-41: Check for duplicate <main> normalized content hashes (except 404 pages)
        if "404" not in rel_p and "_not-found" not in rel_p:
            norm_content = re.sub(r'\s+', '', main_inner)
            if norm_content in main_hashes:
                print(f"❌ AC-41 Violation: Duplicate <main> content found between {rel_p} and {main_hashes[norm_content]}")
                return False
            main_hashes[norm_content] = rel_p

    # 2) Check source for forbidden escaped arbitrary classes (AC-26)
    forbidden_terms = ["w-screen", "-mx-[50vw]", "min-[1240px]:"]
    for root_dir in [os.path.join(REFER_DIR, "app"), os.path.join(REFER_DIR, "components")]:
        for root, _, files in os.walk(root_dir):
            for fname in files:
                if fname.endswith((".ts", ".tsx")):
                    fpath = os.path.join(root, fname)
                    with open(fpath, "r", encoding="utf-8") as f:
                        content = f.read()
                    for term in forbidden_terms:
                        if term in content:
                            print(f"❌ Forbidden term '{term}' found in {fpath}")
                            return False

    print("✅ Gate 8 PASSED: Layout 3-tier architecture, route uniqueness (AC-41), and AC-25/AC-26 no-escape rule verified")
    return True

def test_token_contracts():
    print("\n--- Gate 9: Color Token Alpha Contract & Header Style Gate (AC-35 / AC-36 / AC-37) ---")
    
    # AC-36: Verify zero commas in --*-rgb definitions in globals.css
    globals_css = os.path.join(REFER_DIR, "app", "globals.css")
    with open(globals_css, "r", encoding="utf-8") as f:
        css_text = f.read()
    
    rgb_lines = re.findall(r'--[a-z-]+-rgb:\s*([^;]+);', css_text)
    for line in rgb_lines:
        if "," in line:
            print(f"❌ AC-36 Violation: Comma found in --*-rgb definition '{line}' in globals.css")
            return False

    # AC-35: Verify tailwind.config.ts has <alpha-value> for color tokens
    tw_config = os.path.join(REFER_DIR, "tailwind.config.ts")
    with open(tw_config, "r", encoding="utf-8") as f:
        tw_text = f.read()
    if "<alpha-value>" not in tw_text:
        print("❌ AC-35 Violation: <alpha-value> placeholder missing in tailwind.config.ts")
        return False

    # AC-37: Verify Header.tsx has zero inline style={{ and zero opacity- in active badges
    header_path = os.path.join(REFER_DIR, "components", "Header.tsx")
    with open(header_path, "r", encoding="utf-8") as f:
        header_text = f.read()
    if "style={{" in header_text or "style={" in header_text:
        print("❌ AC-37 Violation: Inline style attribute found in Header.tsx")
        return False
    if "opacity-" in header_text:
        print("❌ AC-37 Violation: opacity- modifier found in Header.tsx active badge elements")
        return False

    print("✅ Gate 9 PASSED: Color token alpha contract, space separation (AC-36), and Header clean styling (AC-37) verified")
    return True

def srgb_luminance(r, g, b):
    def adjust(c):
        c = c / 255.0
        return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4
    return 0.2126 * adjust(r) + 0.7152 * adjust(g) + 0.0722 * adjust(b)

def contrast_ratio(rgb1, rgb2):
    l1 = srgb_luminance(*rgb1)
    l2 = srgb_luminance(*rgb2)
    return (max(l1, l2) + 0.05) / (min(l1, l2) + 0.05)

def test_color_contrast_gate():
    print("\n--- Gate 10: WCAG 2.1 AA Color Contrast & Whitelist Gate (AC-43 / AC-44 / AC-45 / AC-47) ---")
    globals_css = os.path.join(REFER_DIR, "app", "globals.css")
    with open(globals_css, "r", encoding="utf-8") as f:
        css_text = f.read()

    # Parse light and dark RGB tokens
    root_match = re.search(r':root\s*\{([^}]+)\}', css_text, flags=re.S)
    dark_match = re.search(r'html\[data-theme="dark"\]\s*\{([^}]+)\}', css_text, flags=re.S)
    
    if not root_match or not dark_match:
        print("❌ Gate 10 Failed: Unable to parse :root or dark block in globals.css")
        return False

    def parse_tokens(block):
        res = {}
        for m in re.finditer(r'--([a-z-]+)-rgb:\s*(\d+)\s+(\d+)\s+(\d+);', block):
            res[m.group(1)] = (int(m.group(2)), int(m.group(3)), int(m.group(4)))
        return res

    light_tokens = parse_tokens(root_match.group(1))
    dark_tokens = parse_tokens(dark_match.group(1))

    # AC-43: Verify contrast thresholds for cobalt-dark and vermillion-dark on ivory and paper
    cd_light = light_tokens.get("cobalt-dark")
    vd_light = light_tokens.get("vermillion-dark")
    ivory_light = light_tokens.get("ivory")
    paper_light = light_tokens.get("paper")

    if not all([cd_light, vd_light, ivory_light, paper_light]):
        print("❌ AC-43 Failed: Missing required light RGB tokens")
        return False

    ratio_cd_ivory = contrast_ratio(cd_light, ivory_light)
    ratio_cd_paper = contrast_ratio(cd_light, paper_light)
    ratio_vd_ivory = contrast_ratio(vd_light, ivory_light)
    ratio_vd_paper = contrast_ratio(vd_light, paper_light)

    if ratio_cd_ivory < 4.5 or ratio_cd_paper < 4.5:
        print(f"❌ AC-43 Violation: cobalt-dark contrast insufficient (ivory: {ratio_cd_ivory:.2f}, paper: {ratio_cd_paper:.2f})")
        return False
    if ratio_vd_ivory < 4.5 or ratio_vd_paper < 4.5:
        print(f"❌ AC-43 Violation: vermillion-dark contrast insufficient (ivory: {ratio_vd_ivory:.2f}, paper: {ratio_vd_paper:.2f})")
        return False

    # AC-45: Verify inversion contrast between light and dark -dark tokens (>= 2.0)
    cd_l = light_tokens.get("cobalt-dark")
    cd_d = dark_tokens.get("cobalt-dark")
    vd_l = light_tokens.get("vermillion-dark")
    vd_d = dark_tokens.get("vermillion-dark")

    if cd_l and cd_d:
        inversion_ratio_cd = contrast_ratio(cd_l, cd_d)
        if inversion_ratio_cd < 2.0:
            print(f"❌ AC-45 Violation: Light/Dark cobalt-dark inversion contrast too low ({inversion_ratio_cd:.2f} < 2.0)")
            return False

    if vd_l and vd_d:
        inversion_ratio_vd = contrast_ratio(vd_l, vd_d)
        if inversion_ratio_vd < 2.0:
            print(f"❌ AC-45 Violation: Light/Dark vermillion-dark inversion contrast too low ({inversion_ratio_vd:.2f} < 2.0)")
            return False

    # AC-44: Verify text-cobalt or text-vermillion (DEFAULT) is not combined with small text sizes
    # AC-47: Verify text-white utility usage <= 8 instances
    # C11-9 (AC-48): Verify zero dark: modifier usage on accent colors (R-8.29)
    white_count = 0
    small_size_pattern = re.compile(r'text-(xs|sm|\[0\.65rem\]|\[0\.78rem\])')
    default_color_pattern = re.compile(r'text-(cobalt|vermillion)(?![a-z-])')
    dark_accent_pattern = re.compile(r'dark:(?:[a-z:-]*)(cobalt|vermillion)')

    for root_dir in [os.path.join(REFER_DIR, "app"), os.path.join(REFER_DIR, "components")]:
        for root, _, files in os.walk(root_dir):
            for fname in files:
                if fname.endswith((".ts", ".tsx")):
                    fpath = os.path.join(root, fname)
                    with open(fpath, "r", encoding="utf-8") as f:
                        content = f.read()

                    # AC-47 count
                    white_count += content.count("text-white")

                    # AC-44 & C11-9 check line by line
                    for line_idx, line in enumerate(content.splitlines(), start=1):
                        # C11-9: Zero dark: modifier on accent colors (allow dark:text-ivory / dark:group-open:text-ivory exceptions)
                        if dark_accent_pattern.search(line):
                            print(f"❌ C11-9 (AC-48) Violation: Forbidden dark: modifier on accent color at {fpath}:{line_idx}\n   {line.strip()}")
                            return False

                        if small_size_pattern.search(line) and default_color_pattern.search(line):
                            # Ignore allowed exceptions (e.g. HeroComposition, Pillar bullets, or explicitly exempted titles)
                            if "Pillar #" in line or "w-1.5 h-1.5" in line or "text-3xl" in line:
                                continue
                            print(f"❌ AC-44 Violation: Small text combined with DEFAULT accent color at {fpath}:{line_idx}\n   {line.strip()}")
                            return False

    if white_count > 8:
        print(f"❌ AC-47 Violation: text-white usage count ({white_count}) exceeds whitelist limit (8)")
        return False

    print(f"✅ Gate 10 & 11 PASSED: Color contrast (AC-43), small text routing (AC-44), inversion (AC-45), text-white count ({white_count}/8, AC-47), and dark: accent modifier zero check (C11-9) verified")
    return True

def main():
    print("=" * 70)
    print(" ANL HOMEPAGE E2E TEST SUITE RUNNER — TIERS 1-4")
    print("=" * 70)

    success = True
    success &= test_clean()
    success &= test_lint()
    success &= test_build()
    success &= test_tsc()
    success &= test_ssg_routes()
    success &= test_data_counts()
    success &= test_theme_tokens()
    success &= test_unittest_suite()
    success &= test_layout_architecture()
    success &= test_token_contracts()
    success &= test_color_contrast_gate()

    print("\n" + "-" * 70)
    if success:
        print("ALL E2E TEST SUITES PASSED CLEAN (Exit Code 0).")
        print("=" * 70)
        sys.exit(0)
    else:
        print("❌ ONE OR MORE E2E GATES FAILED.")
        print("=" * 70)
        sys.exit(1)

if __name__ == "__main__":
    main()
