#!/usr/bin/env python3
"""Retry the 8 failed models with higher timeout."""

import os
import sys
import time
from pathlib import Path

# Load .env
env_file = Path(__file__).resolve().parent.parent.parent / ".env"
if env_file.exists():
    for line in env_file.read_text().splitlines():
        line = line.strip()
        if line and not line.startswith("#") and "=" in line:
            k, v = line.split("=", 1)
            os.environ.setdefault(k.strip(), v.strip())

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from evalytic.bench.runner import run_bench
from evalytic.bench.registry import MODEL_REGISTRY

FAILED = [
    "nano-banana-2",   # service overloaded (geçici)
    "imagen-3",        # timeout
    "hunyuan-3",       # timeout
    "gpt-image-1",     # timeout
    "gpt-image-1-mini",# timeout
    "glm-image",       # timeout
    "hidream",         # timeout
    "sd35-large",      # timeout
]

PROMPT = "A glowing neon sign reading EVALYTIC in a dark rainy alley, reflections on wet cobblestones, cinematic lighting"

print(f"=== Retry: {len(FAILED)} failed models (timeout=600s) ===")
print(f"FAL_KEY: {'set' if os.environ.get('FAL_KEY') else 'MISSING!'}")
print()

start = time.monotonic()

report = run_bench(
    models=FAILED,
    prompts=[{"prompt": PROMPT}],
    seed=42,
    no_judge=True,
    concurrency=8,
    timeout=600,
    name="leaderboard-retry",
    verbose=True,
)

elapsed = time.monotonic() - start

print(f"\n=== Retry Results ({elapsed:.0f}s) ===\n")
print(f"{'Model':<22} {'Status':<8} {'Time (s)':<10} {'Sharp':<10} {'Error'}")
print("-" * 80)

for item in report.items:
    for model, ir in item.results.items():
        status = "OK" if ir.status == "success" else "FAIL"
        gen_sec = f"{ir.generation_time_ms / 1000:.1f}" if ir.generation_time_ms else "-"
        sharp = "-"
        if ir.metrics:
            for m in ir.metrics:
                if m.metric == "sharpness":
                    sharp = f"{m.value:.3f}"
        err = ir.error[:60] if ir.error else ""
        print(f"{model:<22} {status:<8} {gen_sec:<10} {sharp:<10} {err}")
