from enum import Enum
from typing import Any

from pydantic import BaseModel


class CostTier(str, Enum):
    SIMPLE = "simple"
    MODERATE = "moderate"
    COMPLEX = "complex"
    EXPERT = "expert"


TIER_ORDER = [CostTier.SIMPLE, CostTier.MODERATE, CostTier.COMPLEX, CostTier.EXPERT]


class ModelConfig(BaseModel):
    name: str
    tier: CostTier
    api_key: str | None = None
    api_base: str | None = None
    extra_params: dict[str, Any] = {}


class RoutingResult(BaseModel):
    tier: CostTier
    model: str
    confidence: float
    router_type: str
    screener_cost: float


class RoutedResponse:
    """Wraps the LiteLLM response with routing metadata."""

    __slots__ = ("response", "routing", "cost", "total_cost", "saved")

    def __init__(
        self,
        response: Any,
        routing: RoutingResult,
        cost: float,
        total_cost: float,
        saved: float,
    ) -> None:
        self.response = response
        self.routing = routing
        self.cost = cost
        self.total_cost = total_cost
        self.saved = saved

    @property
    def content(self) -> str | None:
        """Shortcut to get the response text."""
        try:
            return self.response.choices[0].message.content
        except (AttributeError, IndexError):
            return None

    @property
    def model(self) -> str:
        """Which model actually handled the request."""
        return self.routing.model

    @property
    def tier(self) -> CostTier:
        """What complexity tier the request was classified as."""
        return self.routing.tier
