#!/usr/bin/env python
"""Smoke tests for crawler.py — fast, background, and vision fallback modes."""

import subprocess
import sys
import time
from pathlib import Path

HERE = Path(__file__).resolve().parent
CRAWLER = HERE / "crawler.py"
SITES_ROOT = HERE.parent / "sites_crawled"
TIMING_DIR = HERE / "timing_logs"

FAST_URLS = [
    "https://www.tt.com",
    "https://www.heise.de/ct",
    "https://www.prolicht.at/en/inspiration/segment/office/",
]

EXPECTED_OUTPUT_DIRS = [
    SITES_ROOT / "www.tt.com",
    SITES_ROOT / "www.heise.de",
    SITES_ROOT / "www.prolicht.at",
]


def run_crawler(args: list[str]) -> subprocess.CompletedProcess:
    cmd = [sys.executable, str(CRAWLER)] + args
    print(f"Running: {' '.join(cmd)}")
    print("=" * 80)
    start = time.time()
    proc = subprocess.run(cmd, cwd=str(HERE), capture_output=False, text=True)
    elapsed = time.time() - start
    print(f"\nWall-clock time: {elapsed:.1f}s")
    return proc


def verify_fast_outputs() -> dict[str, bool]:
    checks: dict[str, bool] = {}
    for out_dir in EXPECTED_OUTPUT_DIRS:
        domain = out_dir.name
        checks[f"{domain}_dir"] = out_dir.exists()
        md_files = list(out_dir.glob("*.md"))
        md_files = [f for f in md_files if "_removed_parts" not in f.name and "_page_index" not in f.name]
        has_md = len(md_files) > 0
        checks[f"{domain}_markdown"] = has_md
        if has_md:
            for mf in md_files:
                size = mf.stat().st_size
                checks[f"{domain}_md_nonempty"] = size > 0
                break
        html_files = list(out_dir.glob("*.html"))
        html_files = [f for f in html_files if "_before_cookie" not in f.name and "_after_cookie" not in f.name]
        checks[f"{domain}_html"] = len(html_files) > 0

        # Verify timing CSV exists
        csv_files = list(TIMING_DIR.glob("*.csv")) if TIMING_DIR.exists() else []
        checks["timing_csv_exists"] = len(csv_files) > 0

    return checks


def verify_fullpage_outputs(out_dir: Path) -> dict[str, bool]:
    checks = {}
    fullpage_md = out_dir / f"{out_dir.name.replace('.', '__')}__fullpage.md"
    checks["fullpage_md_exists"] = fullpage_md.exists()
    return checks


def print_results(checks: dict[str, bool]) -> int:
    failures = 0
    for name, ok in sorted(checks.items()):
        status = "PASS" if ok else "FAIL"
        print(f"  {status} {name}")
        if not ok:
            failures += 1
    print(f"\n{len(checks)} checks: {len(checks) - failures} pass, {failures} fail")
    return 1 if failures else 0


def run_fast_smoke() -> int:
    print("\n" + "=" * 80)
    print("FAST SMOKE: 3 URLs, no images, no screenshots")
    print("=" * 80)
    result = run_crawler([
        "--fetch-urls", *FAST_URLS,
        "--threads", "3",
        "--headless",
        "--markdown", "true",
        "--images", "false",
        "--screenshots", "false",
        "--quality-check", "false",
        "--verbose",
    ])
    checks = verify_fast_outputs()
    exit_code = print_results(checks)
    if result.returncode != 0:
        print(f"\n[WARN] Crawler exited with code {result.returncode}")
        return 3
    return exit_code


def run_background_smoke() -> int:
    print("\n" + "=" * 80)
    print("BACKGROUND SMOKE: single URL, full-page background")
    print("=" * 80)
    result = run_crawler([
        "--fetch-single", "https://www.prolicht.at",
        "--headless",
        "--markdown", "true",
        "--images", "true",
        "--screenshots", "false",
        "--quality-check", "false",
        "--fullpage-background", "true",
        "--fullpage-output", "second",
        "--max-images", "5",
        "--verbose",
    ])
    out_dir = SITES_ROOT / "www.prolicht.at"
    checks = verify_fullpage_outputs(out_dir)
    exit_code = print_results(checks)
    if result.returncode != 0:
        print(f"\n[WARN] Crawler exited with code {result.returncode}")
        return 3
    return exit_code


def main() -> int:
    import argparse
    p = argparse.ArgumentParser(description="Crawler smoke tests")
    p.add_argument("--mode", default="fast", choices=["fast", "background", "all"],
                   help="Which smoke test to run")
    args = p.parse_args()

    mode = args.mode
    exit_code = 0

    if mode in ("fast", "all"):
        exit_code = run_fast_smoke()

    if mode in ("background", "all"):
        bg_code = run_background_smoke()
        if bg_code != 0 and exit_code == 0:
            exit_code = bg_code

    return exit_code


if __name__ == "__main__":
    raise SystemExit(main())
