"""Template API schemas."""

from __future__ import annotations

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

from pydantic import BaseModel, Field

# ── Segment ──


class SegmentResponse(BaseModel):
    id: str
    template_id: str
    segment_order: int
    duration_min: float
    duration_max: float
    material_type: Optional[str] = None
    created_at: datetime
    updated_at: datetime


class SegmentRequest(BaseModel):
    segment_order: int
    duration_min: float
    duration_max: float
    material_type: Optional[str] = None


# ── Template Response ──


class TemplateResponse(BaseModel):
    id: str
    user_id: str
    name: str
    mode: str
    category: str = ""
    tags: List[str] = Field(default_factory=list)
    title_config: Dict[str, Any] = Field(default_factory=dict)
    subtitle_config: Dict[str, Any] = Field(default_factory=dict)
    bgm_config: Dict[str, Any] = Field(default_factory=dict)
    estimated_duration: float = 0.0
    segments: List[SegmentResponse] = Field(default_factory=list)
    is_active: bool = True
    is_favorite: bool = False
    created_at: datetime
    updated_at: datetime


class ToggleFavoriteResponse(BaseModel):
    id: str
    is_favorite: bool


class ListTemplatesResponse(BaseModel):
    items: List[TemplateResponse]
    total: int = 0


# ── Template Request ──


class CreateTemplateRequest(BaseModel):
    name: str
    mode: str
    category: str = ""
    tags: List[str] = Field(default_factory=list)
    title_config: Dict[str, Any] = Field(default_factory=dict)
    subtitle_config: Dict[str, Any] = Field(default_factory=dict)
    bgm_config: Dict[str, Any] = Field(default_factory=dict)
    estimated_duration: float = 0.0
    segments: List[SegmentRequest] = Field(default_factory=list)


class UpdateTemplateRequest(BaseModel):
    name: Optional[str] = None
    mode: Optional[str] = None
    category: Optional[str] = None
    tags: Optional[List[str]] = None
    title_config: Optional[Dict[str, Any]] = None
    subtitle_config: Optional[Dict[str, Any]] = None
    bgm_config: Optional[Dict[str, Any]] = None
    estimated_duration: Optional[float] = None
    segments: Optional[List[SegmentRequest]] = None


# ── Validate ──


class ValidateTemplateRequest(BaseModel):
    voiceover_duration: Optional[float] = None  # 配音实际时长（秒）


class GenerateWarningResponse(BaseModel):
    code: str
    message: str
    details: Dict[str, Any] = Field(default_factory=dict)


class ValidateTemplateResponse(BaseModel):
    template: TemplateResponse
    warnings: List[GenerateWarningResponse] = Field(default_factory=list)


# ── Category ──


class CategoryResponse(BaseModel):
    id: str
    user_id: str
    name: str
    created_at: datetime


class CreateCategoryRequest(BaseModel):
    name: str


class ListCategoriesResponse(BaseModel):
    items: List[CategoryResponse]
