#!/usr/bin/env python
import argparse
import copy
import json
import sys
from datetime import datetime

from dotenv import load_dotenv
from smolagents import InferenceClientModel, LiteLLMModel, OpenAIServerModel
from tqdm import tqdm

from screensuite import registry
from screensuite.basebenchmark import BaseBenchmark, EvaluationConfig

load_dotenv()


def get_user_input_interactive():
    """Get user input interactively when no arguments are passed"""

    # Welcome header with style
    print("\n" + "=" * 80)
    print("Running ScreenSuite".center(80))
    print("=" * 80 + "\n")

    # Get available benchmarks
    all_benchmarks = registry.list_all()

    # Benchmarks section
    print("📊 BENCHMARK CHOICE")

    # Display benchmarks in a nice table format
    for i, benchmark in enumerate(all_benchmarks, 1):
        print(f"  {i:2d}. {benchmark.name}")

    print("─" * 40)

    # Get benchmark selection with validation loop
    selected_benchmarks: list[BaseBenchmark] = []
    while not selected_benchmarks:
        print("┌─" + "─" * 50 + "─┐")
        print("│ " + "Choose your benchmark(s):".ljust(50) + " │")
        print("│ " + "• Type a benchmark name from above".ljust(50) + " │")
        print("│ " + "• Type 'all' for all benchmarks".ljust(50) + " │")
        print("│ " + "• Type numbers (e.g., '1,3,5')".ljust(50) + " │")
        print("└─" + "─" * 50 + "─┘")

        benchmark_choice = input("\n👉 Your choice: ").strip()

        if not benchmark_choice:
            print("❌ Please enter a valid choice.")
            continue

        # Process benchmark choice
        if benchmark_choice.lower() == "all":
            selected_benchmarks = all_benchmarks
            print(f"✅ Selected ALL {len(selected_benchmarks)} benchmarks!")
        elif "," in benchmark_choice:
            # Handle number selection
            try:
                indices = [int(x.strip()) - 1 for x in benchmark_choice.split(",")]
                valid_indices = [i for i in indices if 0 <= i < len(all_benchmarks)]
                if valid_indices and len(valid_indices) == len(indices):
                    selected_benchmarks = [all_benchmarks[i] for i in valid_indices]
                    print(f"✅ Selected {len(selected_benchmarks)} benchmarks!")
                else:
                    print("❌ Invalid benchmark numbers. Please check the list and try again.")
            except ValueError:
                print("❌ Invalid input format. Please enter numbers separated by commas.")
        elif benchmark_choice.isdigit():
            # Single number
            try:
                idx = int(benchmark_choice) - 1
                if 0 <= idx < len(all_benchmarks):
                    selected_benchmarks = [all_benchmarks[idx]]
                    print(f"✅ Selected: {selected_benchmarks[0].name}")
                else:
                    print(f"❌ Invalid number. Please choose between 1 and {len(all_benchmarks)}.")
            except ValueError:
                print("❌ Invalid number format.")
        else:
            # Try to match by name
            matched = [b for b in all_benchmarks if b.name == benchmark_choice]
            if matched:
                selected_benchmarks = matched
                print(f"✅ Selected: {matched[0].name}")
            else:
                print(f"❌ Benchmark '{benchmark_choice}' not found. Please check the list and try again.")

    # Inference type selection
    print("\n⚡️ INFERENCE TYPE CHOICE")
    print("┌─" + "─" * 30 + "─┐")
    print("│ " + "Inference Types:".ljust(30) + " │")
    print("│ " + "1. InferenceClient".ljust(30) + " │")
    print("│ " + "2. OpenAI Server".ljust(30) + " │")
    print("│ " + "3. LiteLLM".ljust(30) + " │")
    print("└─" + "─" * 30 + "─┘")

    inference_choice = input("👉 Choose inference type (1-3) [default: 1]: ").strip()
    inference_map = {"1": "InferenceClient", "2": "OpenAIServer", "3": "LiteLLM", "": "InferenceClient"}
    inference_type = inference_map.get(inference_choice, "InferenceClient")
    print(f"✅ Inference type: {inference_type}")

    # Get model ID
    print("\n🧠 MODEL CHOICE")
    print("─" * 40)
    default_model = "Qwen/Qwen2.5-VL-32B-Instruct"
    model_id = input(f"👉 Model ID [default: {default_model}]: ").strip()
    if not model_id:
        model_id = default_model
    print(f"✅ Model: {model_id}")

    # Get provider (only needed for InferenceClient)
    provider = None
    if inference_type == "InferenceClient":
        print("\n🔌 PROVIDER CHOICE")
        print("─" * 40)
        provider = input("👉 Provider (required for InferenceClient): ").strip()
        while not provider:
            print("❌ Provider is required for InferenceClient!")
            provider = input("👉 Enter provider: ").strip()
        print(f"✅ Provider: {provider}")

    # Performance settings
    print("\n⚙️  PERFORMANCE SETTINGS")
    print("─" * 40)

    # Get parallel workers
    parallel_workers_input = input("👉 Max parallel workers [default: 3]: ").strip()
    try:
        parallel_workers = int(parallel_workers_input) if parallel_workers_input else 3
        print(f"✅ Parallel workers: {parallel_workers}")
    except ValueError:
        parallel_workers = 3
        print("❌ Invalid input. Using default: 3")

    # Get max samples
    max_samples_input = input("👉 Max samples to test [default: 500]: ").strip()
    try:
        max_samples = int(max_samples_input) if max_samples_input else 500
        print(f"✅ Max samples: {max_samples}")
    except ValueError:
        max_samples = 500
        print("❌ Invalid input. Using default: 500")

    # Get run name
    print("\n📝 RUN NAME")
    print("─" * 40)
    default_run_name = f"{model_id.replace('/', '-')}_{datetime.now().strftime('%Y-%m-%d')}"
    run_name = input(f"👉 Run name [default: {default_run_name}]: ").strip()
    if not run_name:
        run_name = default_run_name
    print(f"✅ Run name: {run_name}")

    # Summary
    print("\n" + "=" * 80)
    print("📋 CONFIGURATION SUMMARY".center(80))
    print("=" * 80)
    print(f"🎯 Benchmarks: {len(selected_benchmarks)} selected")
    for i, benchmark in enumerate(selected_benchmarks[:5], 1):  # Show first 5
        print(f"   {i}. {benchmark.name}")
    if len(selected_benchmarks) > 5:
        print(f"   ... and {len(selected_benchmarks) - 5} more")
    print(f"⚡️ Inference: {inference_type}")
    print(f"🧠 Model: {model_id}")
    if provider:
        print(f"🔌 Provider: {provider}")
    print(f"⚡ Workers: {parallel_workers}")
    print(f"📊 Samples: {max_samples}")
    print(f"📝 Run: {run_name}")
    print("=" * 80)

    print("\n➡️ Starting benchmark evaluation...")

    return {
        "benchmarks": selected_benchmarks,
        "inference_type": inference_type,
        "model_id": model_id,
        "provider": provider,
        "parallel_workers": parallel_workers,
        "max_samples": max_samples,
        "run_name": run_name,
    }


def launch_test(model, benchmarks, original_evaluation_config):
    evaluation_config = copy.deepcopy(original_evaluation_config)  # NOTE: important!
    if model.model_id is None:
        model_name = "custom-endpoint"
    elif model.model_id.startswith("http"):
        model_name = model.model_id.split("/")[-1][:10]
    else:
        model_name = model.model_id

    if evaluation_config.run_name is None:
        evaluation_config.run_name = f"{model_name.replace('/', '-')}_{datetime.now().strftime('%Y-%m-%d')}"

    print(f"===== Running evaluation under name: {evaluation_config.run_name} =====")

    # Load already processed benchmarks from results.jsonl
    output_results_file = f"results_{evaluation_config.run_name}.jsonl"
    processed_benchmarks = set()
    try:
        with open(output_results_file, "r") as f:
            for line in f:
                try:
                    data = json.loads(line)
                    if "benchmark_name" in data:
                        processed_benchmarks.add(data["benchmark_name"])
                except json.JSONDecodeError:
                    continue
    except FileNotFoundError:
        pass

    print("-> Found these processed benchmarks: ", processed_benchmarks)

    for benchmark in tqdm(sorted(benchmarks, key=lambda b: b.name), desc="Running benchmarks"):
        if benchmark.name in processed_benchmarks:
            print(f"Skipping already processed benchmark: {benchmark.name}")
            continue
        if "multistep" not in benchmark.tags:
            print("=" * 100)
            print(f"Running benchmark: {benchmark.name}")
            try:
                benchmark.load()

                results = benchmark.evaluate(
                    model,
                    evaluation_config,
                )
                print(f"Results for {benchmark.name}: {results}")

                # Save metrics to JSONL file
                metrics_entry = {"benchmark_name": benchmark.name, "metrics": results._metrics}
                with open(output_results_file, "a") as f:
                    f.write(json.dumps(metrics_entry) + "\n")
            except Exception as e:
                print(f"Error running benchmark {benchmark.name}: {e}")
                continue


def main():
    parser = argparse.ArgumentParser(description="Run benchmarks with optional run name")
    parser.add_argument("--run-name", type=str, help="Name of the run to continue or create", default=None)
    parser.add_argument(
        "--tag",
        type=str,
        nargs="+",
        help="Tags to filter benchmarks (can provide multiple tags)",
        default=["to_evaluate"],
    )
    parser.add_argument("--name", type=str, help="Name of the benchmark to run", default=None)
    parser.add_argument("--parallel-workers", type=int, help="Number of parallel workers", default=3)
    parser.add_argument("--max-samples-to-test", type=int, help="Number of samples to test", default=500)
    parser.add_argument("--model-id", type=str, help="Model ID to use", default="Qwen/Qwen2.5-VL-32B-Instruct")
    parser.add_argument("--provider", type=str, help="Provider to use", default=None)
    parser.add_argument(
        "--inference-type",
        type=str,
        help="Inference type to use",
        choices=["InferenceClient", "OpenAIServer", "LiteLLM"],
        default="InferenceClient",
    )

    # Check if no arguments were passed (only script name)
    if len(sys.argv) == 1:
        # Interactive mode
        interactive_config = get_user_input_interactive()

        # Use interactive config
        all_benchmarks = interactive_config["benchmarks"]
        args_model_id = interactive_config["model_id"]
        args_provider = interactive_config["provider"]
        args_inference_type = interactive_config["inference_type"]
        args_parallel_workers = interactive_config["parallel_workers"]
        args_max_samples_to_test = interactive_config["max_samples"]
        args_run_name = interactive_config["run_name"]

    else:
        # Command line mode
        args = parser.parse_args()

        # Get all registered benchmarks
        if not args.tag:  # This handles both empty list and None
            all_benchmarks = registry.list_all()
        else:
            all_benchmarks = registry.get_by_tags(tags=args.tag, match_all=False)

        if args.name:
            all_benchmarks = [benchmark for benchmark in all_benchmarks if benchmark.name == args.name]

        args_model_id = args.model_id
        args_provider = args.provider
        args_inference_type = args.inference_type
        args_parallel_workers = args.parallel_workers
        args_max_samples_to_test = args.max_samples_to_test
        args_run_name = args.run_name

    if args_run_name is None:
        args_run_name = f"{args_model_id.replace('/', '-')}_{datetime.now().strftime('%Y-%m-%d')}"

    evaluation_config = EvaluationConfig(
        test_mode=False,
        parallel_workers=args_parallel_workers,
        max_samples_to_test=args_max_samples_to_test,
        run_name=args_run_name,
    )

    if args_inference_type == "InferenceClient":
        if args_provider is None:
            raise ValueError("Provider is required for InferenceClient")
        model = InferenceClientModel(
            model_id=args_model_id,
            provider=args_provider,
            max_tokens=4096,
        )
    elif args_inference_type == "OpenAIServer":
        model = OpenAIServerModel(
            model_id=args_model_id,
            max_tokens=4096,
        )
    elif args_inference_type == "LiteLLM":
        model = LiteLLMModel(
            model_id=args_model_id,
            max_tokens=4096,
        )
    else:
        raise ValueError(f"Invalid inference type: {args_inference_type}")

    launch_test(model, all_benchmarks, evaluation_config)


if __name__ == "__main__":
    main()
