from pydantic import BaseModel, Field, model_validator


class ConfirmGenerationRequest(BaseModel):
    """确认生成请求体 — 基于预览任务创建正式生成任务"""

    output_width: int = Field(default=1080, description="输出视频宽度")
    output_height: int = Field(default=1920, description="输出视频高度")
    cover_url: str = Field(default="", description="自定义封面图片 URL")
    custom_title: str = Field(default="", description="自定义视频标题")


class CreateGenerationTaskRequest(BaseModel):
    """创建生成任务请求。

    支持两种模式（至少提供一种）：
    - 项目模式：project_id + asset_library_id（向后兼容）
    - 模板模式：template_id + asset_ids / title_ids / voice_ids
    """

    project_id: str = ""
    asset_library_id: str = ""
    strategy_id: str = ""
    voice_library_id: str = ""
    created_by_user_id: str = ""
    # ── 模板模式新增字段 ──
    template_id: str = ""
    asset_ids: list[str] = Field(default_factory=list)
    title_ids: list[str] = Field(default_factory=list)
    voice_ids: list[str] = Field(default_factory=list)
    # ── 来源剪辑计划 ──
    source_edit_plan_id: str = ""
    # ── 批量生成 ──
    count: int = Field(default=1, ge=1, le=50, description="批量生成数量，默认1，最大50")
    # ── 素材库自动匹配 ──
    asset_select_mode: str = Field(
        default="all",
        description="素材选取模式：all=全部ready视频, random=随机选取, smart=智能匹配（按质量/时长评分）",
    )
    asset_select_count: int = Field(
        default=0, ge=0, le=100, description="选取数量，0表示全部（仅 random/smart 模式有效）"
    )
    # ── 预览 / 确认生成 ──
    is_preview: bool = Field(default=True, description="是否为预览任务")
    source_task_id: str = Field(default="", description="来源预览任务 ID（确认生成时传入）")
    output_width: int = Field(default=1280, description="输出视频宽度")
    output_height: int = Field(default=720, description="输出视频高度")
    cover_url: str = Field(default="", description="封面图片 URL")
    custom_title: str = Field(default="", description="自定义视频标题")

    @model_validator(mode="after")
    def _check_at_least_one_mode(self) -> "CreateGenerationTaskRequest":
        has_project = bool(self.project_id.strip())
        has_template = bool(self.template_id.strip())
        if not has_project and not has_template:
            raise ValueError("project_id 或 template_id 至少需要提供一个")
        has_library = bool(self.asset_library_id.strip())
        has_assets = bool(self.asset_ids or self.title_ids or self.voice_ids)
        if not has_library and not has_assets:
            raise ValueError("asset_library_id 或 asset_ids/title_ids/voice_ids 至少需要提供一个")
        return self


class GenerationTaskResponse(BaseModel):
    id: str
    project_id: str
    asset_library_id: str
    strategy_id: str
    voice_library_id: str
    template_id: str = ""
    asset_ids: list[str] = Field(default_factory=list)
    title_ids: list[str] = Field(default_factory=list)
    voice_ids: list[str] = Field(default_factory=list)
    source_edit_plan_id: str = ""
    asset_select_mode: str = ""
    batch_id: str = ""
    is_preview: bool = True
    source_task_id: str = ""
    output_width: int = 1280
    output_height: int = 720
    cover_url: str = ""
    custom_title: str = ""
    status: str
    progress: float
    result_count: int
    error_message: str


class BatchGenerationTaskResponse(BaseModel):
    """批量生成任务响应。"""

    items: list[GenerationTaskResponse]
    total: int


class ListGenerationTasksResponse(BaseModel):
    """用户级生成任务列表响应（跨 project）。"""

    items: list[GenerationTaskResponse]
