"""Model alias resolver (P0+)."""

from __future__ import annotations

from dataclasses import dataclass
from typing import Dict, Optional

from .settings import get_gateway_settings


@dataclass(frozen=True)
class ModelAlias:
    provider: str
    canonical_model: str
    provider_models: Dict[str, str] | None = None
    notes: str | None = None


def _default_aliases() -> dict[str, ModelAlias]:
    # Minimal, extensible static map. Canonical ids reflect common usage.
    aliases: dict[str, ModelAlias] = {
        # OpenAI GPT-5 family
        "gpt-5": ModelAlias(provider="openai", canonical_model="gpt-5"),
        "gpt-5-codex": ModelAlias(provider="openai", canonical_model="gpt-5-codex"),
        "gpt-5-mini": ModelAlias(provider="openai", canonical_model="gpt-5-mini"),
        "gpt-5-nano": ModelAlias(provider="openai", canonical_model="gpt-5-nano"),
        "gpt-5-chat-latest": ModelAlias(provider="openai", canonical_model="gpt-5-chat-latest"),
        # Anthropic Claude 4.1 family (use full ids where known)
        "claude-opus-4-1": ModelAlias(
            provider="anthropic", canonical_model="claude-opus-4-1-20250805"
        ),
        "claude-opus-4": ModelAlias(provider="anthropic", canonical_model="claude-opus-4-20250514"),
        "claude-sonnet-4": ModelAlias(
            provider="anthropic", canonical_model="claude-sonnet-4-20250514"
        ),
        # Google Gemini 2.5 family
        "gemini-2.5-pro": ModelAlias(provider="google", canonical_model="gemini-2.5-pro"),
        "gemini-2.5-flash": ModelAlias(provider="google", canonical_model="gemini-2.5-flash"),
        "gemini-2.5-flash-lite": ModelAlias(
            provider="google", canonical_model="gemini-2.5-flash-lite"
        ),
    }

    # Best-of-N router meta-models: "eb1" and "eb1-pro"
    # These aliases route to the Takumi-hosted EB1 systems.
    aliases["eb1"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb1",
        notes="Takumi EB1 compound AI system",
    )
    aliases["eb1-a"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb1-a",
        notes="Takumi EB1-A adaptive router runtime",
    )
    aliases["eb1-codex"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb1-codex",
        provider_models={
            "openai": "gpt-5-codex",
        },
        notes="Takumi EB1 Codex compound AI system",
    )
    aliases["eb1-codex-high"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb1-codex-high",
        provider_models={
            "openai": "gpt-5-codex",
        },
        notes="Takumi EB1 Codex high tier compound AI system",
    )
    aliases["eb1-pro"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb1-pro",
        notes="Takumi EB1-Pro advanced compound AI system",
    )
    aliases["eb1-fast"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb1-fast",
        notes="Takumi EB1-Fast latency-optimized ensemble",
    )
    aliases["eb1-preview"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb1-preview",
        notes="EB1 Preview with GNN-based tiered routing",
    )
    aliases["eb1-delta-preview"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb1-delta-preview",
        notes="EB1 Delta Preview with LLM conductor for hard queries",
    )
    aliases["eb1-fast-preview"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb1-fast-preview",
        notes="EB1 Fast Preview with GNN-routing, latency optimized",
    )
    aliases["eb1-frontier-preview"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb1-frontier-preview",
        notes="EB1 Frontier Preview with max quality ensemble and extended thinking",
    )
    aliases["eb1-swe-preview"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb1-swe-preview",
        notes="EB1 SWE Preview with complexity-routed SWE-bench optimization",
    )
    aliases["eb1-swe-fast-preview"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb1-swe-fast-preview",
        notes="EB1 SWE Fast Preview with latency-optimized SWE-bench routing",
    )
    aliases["eb1-swe-pro-preview"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb1-swe-pro-preview",
        notes="EB1 SWE Pro Preview with max accuracy SWE-bench routing",
    )
    aliases["eb2"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb2",
        notes="Takumi EB2 operator-driven ensemble",
    )
    aliases["eb2-codex"] = ModelAlias(
        provider="takumi",
        canonical_model="takumi/eb2-codex",
        notes="Takumi EB2 Codex ensemble with Anthropic judge",
    )

    return aliases


_ALIASES: dict[str, ModelAlias] = _default_aliases()


def resolve_model_alias(model: str) -> Optional[ModelAlias]:
    """Resolve a model name to an alias (provider + canonical id)."""
    if not get_gateway_settings().features.model_alias:
        return None
    key = (model or "").strip().lower()
    if key.startswith("takumi/"):
        key = key.split("/", 1)[1]
    return _ALIASES.get(key)
