#!/usr/bin/env python3
"""
SAB-BENCH CLI — Command-line interface for benchmark execution.

Usage:
    sab-bench run --config CONFIG [--real-models] [--enable-cuda]
    sab-bench report --output OUTPUT
    sab-bench list-configs
"""

import click
import json
from pathlib import Path
from datetime import datetime
from rich.console import Console
from rich.table import Table
from rich.progress import Progress, SpinnerColumn, TextColumn

from .config import BenchmarkConfig
from .harness import BenchmarkHarness

console = Console()


@click.group()
@click.version_option(version="0.1.0")
def main():
    """SAB-BENCH — Scalable Alignment Benchmark."""
    pass


@main.command()
@click.option("--config", "-c", type=click.Path(exists=True), required=True,
              help="Path to benchmark config YAML")
@click.option("--real-models", is_flag=True,
              help="Use real model APIs (requires API keys)")
@click.option("--enable-cuda", is_flag=True,
              help="Enable CUDA acceleration if available")
@click.option("--output", "-o", type=click.Path(),
              help="Override output path")
def run(config: str, real_models: bool, enable_cuda: bool, output: str):
    """Run a benchmark."""
    console.print(f"[bold blue]SAB-BENCH v0.1[/bold blue]")
    console.print(f"Loading config: {config}")
    
    # Load config
    cfg = BenchmarkConfig.from_yaml(Path(config))
    
    # Override settings
    if enable_cuda:
        cfg.evaluator.cuda_enabled = True
    if output:
        cfg.output.path = output
    
    # Force stub models if --real-models not set
    if not real_models:
        for model in cfg.evaluator.models:
            if model.provider != "stub":
                console.print(f"[yellow]Warning: Forcing {model.provider}/{model.model} → stub (use --real-models for real APIs)[/yellow]")
                model.provider = "stub"
    
    # Create harness
    harness = BenchmarkHarness(cfg)
    
    # Run benchmark
    console.print(f"[bold green]Running benchmark: {cfg.name}[/bold green]")
    
    with Progress(
        SpinnerColumn(),
        TextColumn("[progress.description]{task.description}"),
        console=console
    ) as progress:
        task = progress.add_task("Evaluating...", total=None)
        
        results = harness.run()
        
        progress.update(task, completed=True)
    
    # Save results
    output_path = Path(cfg.output.path.format(
        timestamp=datetime.utcnow().strftime("%Y%m%d_%H%M%S"),
        name=cfg.name
    ))
    output_path.parent.mkdir(parents=True, exist_ok=True)
    
    with open(output_path, 'w') as f:
        json.dump(results, f, indent=2, default=str)
    
    console.print(f"[bold green]✓ Results saved to: {output_path}[/bold green]")
    
    # Print summary
    print_summary(results)


@main.command()
@click.option("--output", "-o", type=click.Path(exists=True), required=True,
              help="Path to benchmark output JSON")
def report(output: str):
    """Display benchmark results."""
    with open(output) as f:
        results = json.load(f)
    
    print_summary(results)


@main.command()
def list_configs():
    """List available benchmark configurations."""
    configs_dir = Path("configs")
    
    if not configs_dir.exists():
        console.print("[yellow]No configs directory found[/yellow]")
        return
    
    table = Table(title="Available Benchmark Configs")
    table.add_column("Name", style="cyan")
    table.add_column("Type", style="magenta")
    table.add_column("Models", style="green")
    
    for config_path in sorted(configs_dir.glob("*.yaml")):
        try:
            cfg = BenchmarkConfig.from_yaml(config_path)
            models = ", ".join([f"{m.provider}/{m.model}" for m in cfg.evaluator.models])
            table.add_row(config_path.stem, cfg.evaluator.type, models)
        except Exception as e:
            table.add_row(config_path.stem, "[red]ERROR[/red]", str(e))
    
    console.print(table)


def print_summary(results: dict):
    """Print benchmark results summary."""
    console.print("\n[bold]Benchmark Results Summary[/bold]")
    
    table = Table()
    table.add_column("Metric", style="cyan")
    table.add_column("Value", style="green")
    
    table.add_row("Benchmark", results.get("benchmark", "unknown"))
    table.add_row("Duration", f"{results.get('duration_seconds', 0):.2f}s")
    
    if "metrics" in results:
        for key, value in results["metrics"].items():
            if isinstance(value, float):
                table.add_row(key, f"{value:.4f}")
            else:
                table.add_row(key, str(value))
    
    console.print(table)
    
    # Show warnings/errors
    if results.get("warnings"):
        console.print(f"\n[yellow]Warnings ({len(results['warnings'])}):[/yellow]")
        for w in results["warnings"]:
            console.print(f"  • {w}")
    
    if results.get("errors"):
        console.print(f"\n[red]Errors ({len(results['errors'])}):[/red]")
        for e in results["errors"]:
            console.print(f"  • {e}")


if __name__ == "__main__":
    main()
