"""Test tier selection for quick / track / comprehensive evaluation."""

from __future__ import annotations

from collections import defaultdict
from pathlib import Path
from typing import Sequence

from claw_bench.core.task_loader import TaskConfig

# Tier definitions
TIER_QUICK = "quick"
TIER_TRACK = "track"
TIER_COMPREHENSIVE = "comprehensive"

VALID_TIERS = (TIER_QUICK, TIER_TRACK, TIER_COMPREHENSIVE)

# Default runs-per-task for each tier
TIER_DEFAULTS: dict[str, dict[str, int]] = {
    TIER_QUICK: {"runs": 1},
    TIER_TRACK: {"runs": 3},
    TIER_COMPREHENSIVE: {"runs": 5},
}


def _load_curated_quick_tasks() -> list[str] | None:
    """Load curated quick-task IDs from config/quick_tasks.yaml if it exists."""
    config_path = Path(__file__).resolve().parent.parent.parent.parent / "config" / "quick_tasks.yaml"
    if not config_path.exists():
        return None
    try:
        import yaml
        with open(config_path) as f:
            data = yaml.safe_load(f)
        return data.get("tasks", []) if data else None
    except Exception:
        return None


def select_quick_tasks(all_tasks: Sequence[TaskConfig]) -> list[TaskConfig]:
    """Select ~1 task per domain for a fast smoke test (~14 tasks).

    Strategy:
    1. If ``config/quick_tasks.yaml`` exists, use its curated list.
    2. Otherwise pick the first L2 task per domain, falling back to L1.

    The selection is deterministic (sorted by domain then task id).
    """
    curated_ids = _load_curated_quick_tasks()
    if curated_ids:
        id_set = set(curated_ids)
        selected = [t for t in all_tasks if t.id in id_set]
        if selected:
            return sorted(selected, key=lambda t: t.id)

    # Algorithmic fallback: one per domain, prefer L2
    by_domain: dict[str, list[TaskConfig]] = defaultdict(list)
    for t in all_tasks:
        by_domain[t.domain].append(t)

    picked: list[TaskConfig] = []
    for domain in sorted(by_domain):
        tasks = sorted(by_domain[domain], key=lambda t: t.id)
        # Prefer the first L2 task
        l2 = [t for t in tasks if t.level == "L2"]
        if l2:
            picked.append(l2[0])
        else:
            # Fallback to L1
            l1 = [t for t in tasks if t.level == "L1"]
            if l1:
                picked.append(l1[0])
            elif tasks:
                picked.append(tasks[0])

    return picked


def select_track_tasks(
    all_tasks: Sequence[TaskConfig],
    domain: str,
) -> list[TaskConfig]:
    """Select all tasks in a single domain for a deep-dive evaluation."""
    return sorted(
        [t for t in all_tasks if t.domain == domain],
        key=lambda t: t.id,
    )
