"""AI-related Pydantic schemas (P3-M1)."""
from typing import Optional, List, Dict, Any
from pydantic import BaseModel, Field


class ChatMessage(BaseModel):
    """Single chat message."""
    role: str = Field(..., description="system/user/assistant")
    content: str


class ChatRequest(BaseModel):
    """Request body for /api/ai/chat."""
    messages: List[ChatMessage]
    model: Optional[str] = None
    temperature: Optional[float] = None
    max_tokens: Optional[int] = None
    system_prompt: Optional[str] = None


class ChatResponse(BaseModel):
    """Response from /api/ai/chat."""
    content: str
    reasoning_content: Optional[str] = None
    model: str
    usage: Dict[str, Any] = Field(default_factory=dict)
    finish_reason: Optional[str] = None


class AIHealthResponse(BaseModel):
    """Response from /api/ai/health."""
    status: str
    configured: bool
    base_url: str
    default_model: str
    available_models: List[Dict[str, Any]] = Field(default_factory=list)
    error: Optional[str] = None


class AICallLogResponse(BaseModel):
    """AI call log entry."""
    id: int
    endpoint: str
    model: str
    status: str
    duration_ms: int
    prompt_tokens: int
    completion_tokens: int
    total_tokens: int
    error: Optional[str] = None
    created_at: str

    class Config:
        from_attributes = True


class AIUsageSummary(BaseModel):
    """AI usage summary."""
    total_calls: int
    success_calls: int
    failed_calls: int
    total_tokens: int
    total_prompt_tokens: int
    total_completion_tokens: int
    total_duration_ms: int
