#!/usr/bin/env python3
"""Smoke test for client video adaptation.

Validates that the bridge, pipeline init, prompt overrides, and aspect
ratio config all work together without hitting any APIs.
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

passed = 0
failed = 0

def check(label, condition, detail=""):
    global passed, failed
    if condition:
        print(f"  PASS  {label}")
        passed += 1
    else:
        print(f"  FAIL  {label} — {detail}")
        failed += 1

print("=== Client Bridge ===")
try:
    from recoil.pipeline._lib.client_bridge import load_client_storyboard, load_client_bible, get_client_refs_dir
    check("import client_bridge", True)
except ImportError as e:
    check("import client_bridge", False, str(e))

try:
    sb = load_client_storyboard("client_utilityvideo", episode=1)
    check("load_client_storyboard returns data", sb is not None)
    check("storyboard has shots", "shots" in sb)
    check("storyboard _source is manifest", sb.get("_source") == "manifest")
    check("first shot ID is EP001_SH01", sb["shots"][0]["id"] == "EP001_SH01")
except Exception as e:
    check("load_client_storyboard", False, str(e))

try:
    bible = load_client_bible("client_utilityvideo")
    check("load_client_bible returns data", bible is not None)
    check("bible has characters", "characters" in bible)
    check("bible _source is client_bible", bible.get("_source") == "client_bible")
    check("mascot character exists", "mascot" in bible.get("characters", {}))
except Exception as e:
    check("load_client_bible", False, str(e))

print("\n=== Prompt Override Presets ===")
try:
    from recoil.pipeline._lib.prompt_engine import _OVERRIDE_PRESETS, _resolve_bypasses
    check("import _OVERRIDE_PRESETS", True)
    check("5 preset modes", len(_OVERRIDE_PRESETS) == 5)
    check("standard bypasses nothing", len(_OVERRIDE_PRESETS["standard"]) == 0)
    check("stylized bypasses film_stock+quality_guard",
          _OVERRIDE_PRESETS["stylized"] == {"film_stock", "quality_guard"})
    check("raw bypasses 4 sections",
          _OVERRIDE_PRESETS["raw"] == {"film_stock", "quality_guard", "shot_footer", "non_human_locks"})
    check("passthrough bypasses 6 sections",
          len(_OVERRIDE_PRESETS["passthrough"]) == 6)

    mock_shot = {"prompt_overrides": {"mode": "stylized"}}
    bypasses = _resolve_bypasses(mock_shot)
    check("_resolve_bypasses returns set for stylized", "film_stock" in bypasses)

    mock_custom = {"prompt_overrides": {"mode": "custom", "bypass": ["lighting", "spatial"]}}
    custom_bypasses = _resolve_bypasses(mock_custom)
    check("custom mode uses explicit bypass list", "lighting" in custom_bypasses and "spatial" in custom_bypasses)

    mock_default = {}
    default_bypasses = _resolve_bypasses(mock_default)
    check("default mode (no overrides) bypasses nothing", len(default_bypasses) == 0)
except Exception as e:
    check("prompt override presets", False, str(e))

print("\n=== Aspect Ratio Config ===")
try:
    from recoil.core.paths import get_config
    cfg = get_config()
    check("starsend config loaded", cfg is not None)
    check("production_aspect_ratio exists", "production_aspect_ratio" in cfg)
    check("default aspect ratio is 9:16", cfg["production_aspect_ratio"] == "9:16")
except Exception as e:
    check("aspect ratio config", False, str(e))

try:
    import json
    from pathlib import Path
    client_cfg_path = Path.home() / "Dropbox/CLAUDE_DATA/recoil/projects/client_utilityvideo/project_config.json"
    if client_cfg_path.exists():
        client_cfg = json.loads(client_cfg_path.read_text())
        check("client project_config loads", True)
        check("client aspect_ratio is 16:9", client_cfg.get("aspect_ratio") == "16:9")
        check("client project_type is client_video", client_cfg.get("project_type") == "client_video")
        check("skip_flash_enrichment is true", client_cfg.get("skip_flash_enrichment") is True)
    else:
        check("client project_config exists", False, f"Not found: {client_cfg_path}")
except Exception as e:
    check("client project config", False, str(e))

print(f"\n{'='*40}")
print(f"RESULTS: {passed} passed, {failed} failed, {passed+failed} total")
if failed > 0:
    sys.exit(1)
