"""Model alias resolver (P0+) -- OSS build.

Provides the same alias resolution interface as the proprietary build but
without Takumi/EB1 model entries.
"""

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"
        ),
    }

    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()
    return _ALIASES.get(key)
