from datetime import datetime
from typing import Any, Dict, List, Optional

from pydantic import BaseModel, Field


class ModelConfigBase(BaseModel):
    name: str
    provider: str = "dashscope"
    api_base: Optional[str] = None
    chat_model: str = "qwen3-max"
    embedding_model: str = "text-embedding-v4"
    is_active: bool = True


class ModelConfigCreate(ModelConfigBase):
    api_key: str = Field(default="")


class ModelConfigUpdate(BaseModel):
    name: Optional[str] = None
    provider: Optional[str] = None
    api_key: Optional[str] = None
    api_base: Optional[str] = None
    chat_model: Optional[str] = None
    embedding_model: Optional[str] = None
    is_active: Optional[bool] = None


class ModelConfigResponse(ModelConfigBase):
    id: int
    user_id: int
    has_api_key: bool
    api_key_masked: str
    last_used_at: Optional[datetime] = None
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class ModelProviderOption(BaseModel):
    provider: str
    label: str
    default_api_base: str
    default_chat_model: str
    default_embedding_model: str
    chat_models: List[str]
    embedding_models: List[str]
    requires_api_key: bool = True
    supports_custom_api_base: bool = True


class ModelProviderOptionsResponse(BaseModel):
    providers: List[ModelProviderOption]
    defaults: Dict[str, Any]
