"""
Model configuration schemas.
"""
from typing import List, Optional, Dict, Any, Literal
from pydantic import BaseModel, Field, ConfigDict


class ModelCapability(BaseModel):
    """Model capabilities definition"""
    supports_first_frame: Optional[bool] = Field(None, alias="supportsFirstFrame")
    supports_last_frame: Optional[bool] = Field(None, alias="supportsLastFrame")
    supports_multi_image: Optional[bool] = Field(None, alias="supportsMultiImage")
    supports_multi_video: Optional[bool] = Field(None, alias="supportsMultiVideo")
    supports_ref_image: Optional[bool] = Field(None, alias="supportsRefImage")
    supports_lora: Optional[bool] = Field(None, alias="supportsLora")

    model_config = ConfigDict(populate_by_name=True)


class ModelConfig(BaseModel):
    """Model configuration"""
    id: str  # Composite ID in format 'provider/model_key'
    model_key: Optional[str] = None  # Original model key without provider prefix
    name: str
    type: Literal['image', 'video', 'llm', 'audio', 'lyrics', 'music']
    provider: str
    provider_name: Optional[str] = Field(None, alias="providerName")  # Display name (e.g. "阿里")
    is_default: bool = Field(False, alias="isDefault")
    enabled: Optional[bool] = False
    capabilities: Optional[ModelCapability] = None
    resolutions: Optional[Dict[str, Any]] = None  # Nested resolution config
    durations: Optional[Dict[str, Any]] = None  # Duration config (values or min/max)
    counts: Optional[Dict[str, Any]] = None  # Count config (min/max or values)
    voices: Optional[List[Dict[str, Any]]] = None  # Voice options for audio models
    description: Optional[str] = None
    max_tokens: Optional[int] = Field(None, alias="maxTokens")
    supported_formats: Optional[List[str]] = Field(None, alias="supportedFormats")

    model_config = ConfigDict(populate_by_name=True)


class ProviderConfig(BaseModel):
    """Provider configuration"""
    id: str
    name: str
    type: Literal['image', 'video', 'audio', 'lyrics', 'music', 'llm']
    enabled: bool
    api_key: Optional[str] = Field(None, alias="apiKey")
    base_url: Optional[str] = Field(None, alias="baseUrl")
    models: List[ModelConfig]


class ModelDefaults(BaseModel):
    """Model defaults configuration"""
    image: Optional[str] = None
    video: Optional[str] = None
    audio: Optional[str] = None
    lyrics: Optional[str] = None
    music: Optional[str] = None
    llm: Optional[str] = None


class ModelsResponse(BaseModel):
    """Models API response format"""
    data: Dict[str, Dict[str, ModelConfig]]
