"""
Review Card Model

审核卡片模型 - 持久化存储的审核任务卡片

与ReviewTask的关系：
- ReviewCard是用户可见的卡片，包含审核任务的所有信息
- ReviewTask是系统内部的审核任务，可能不持久化
- 一个ReviewCard可能包含多个ReviewTask（如多集审核）
- 过程中审核完成后转换为ReviewCard持久化存储
"""
from pydantic import BaseModel, Field
from typing import Dict, List, Optional, Any
from datetime import datetime
from enum import Enum


class ReviewCardType(str, Enum):
    """审核卡片类型"""
    PROCESS_AUTO = "process_auto"           # 过程中Agent自主识别
    POST_CREATION_AUTO = "post_creation_auto"  # 生成后自动审核
    USER_REQUEST_EPISODE = "user_episode"   # 用户请求单集审核
    USER_REQUEST_MULTIPLE = "user_multiple" # 用户请求多集审核
    USER_REQUEST_ALL = "user_all"           # 用户请求全集审核


class ReviewCardStatus(str, Enum):
    """审核卡片状态"""
    PENDING = "pending"                # 待处理
    AWAITING_USER = "awaiting_user"    # 待确认
    APPROVED = "approved"              # 已通过
    REJECTED = "rejected"              # 已驳回


class ReviewCard(BaseModel):
    """
    审核卡片 - 持久化存储
    """
    # 基础信息
    id: str = Field(..., description="卡片ID")
    project_id: str = Field(..., description="项目ID")
    episode_id: Optional[str] = Field(None, description="剧集ID（单集审核）")
    episode_numbers: List[int] = Field(default_factory=list, description="涉及集数列表（多集审核）")

    # 卡片类型和来源
    card_type: ReviewCardType = Field(..., description="卡片类型")
    trigger_source: str = Field(..., description="触发来源: agent_auto/user_configured/manual")

    # 审核内容
    episode_outline: Optional[str] = Field(None, description="剧集大纲")
    episode_content: Optional[str] = Field(None, description="剧集内容")
    review_reason: str = Field(..., description="审核原因")

    # 审核结果
    issues: List[Dict[str, Any]] = Field(default_factory=list, description="问题列表")
    overall_score: Optional[float] = Field(None, description="总体分数")
    severity: str = Field("medium", description="严重程度: low/medium/high")

    # 审核配置
    review_config_id: Optional[str] = Field(None, description="使用的审核配置ID")
    applied_skills: List[str] = Field(default_factory=list, description="应用的Skills")
    applied_rules: List[str] = Field(default_factory=list, description="应用的规则")

    # 用户操作
    status: ReviewCardStatus = Field(default=ReviewCardStatus.PENDING, description="卡片状态")
    user_comment: Optional[str] = Field(None, description="用户评论")
    modified_content: Optional[str] = Field(None, description="修改后的内容")

    # 时间戳
    created_at: datetime = Field(default_factory=datetime.now, description="创建时间")
    updated_at: datetime = Field(default_factory=datetime.now, description="更新时间")
    completed_at: Optional[datetime] = Field(None, description="完成时间")

    # 附加信息
    metadata: Dict[str, Any] = Field(default_factory=dict, description="附加元数据")

    class Config:
        use_enum_values = False


class ReviewCardCreate(BaseModel):
    """创建审核卡片请求"""
    project_id: str
    episode_id: Optional[str] = None
    episode_numbers: List[int] = Field(default_factory=list)
    card_type: ReviewCardType
    trigger_source: str
    review_reason: str
    episode_outline: Optional[str] = None
    episode_content: Optional[str] = None
    review_config_id: Optional[str] = None
    severity: str = "medium"
    issues: List[Dict[str, Any]] = Field(default_factory=list)
    overall_score: Optional[float] = None


class ReviewCardUpdate(BaseModel):
    """更新审核卡片请求"""
    status: Optional[ReviewCardStatus] = None
    user_comment: Optional[str] = None
    modified_content: Optional[str] = None
    issues: Optional[List[Dict[str, Any]]] = None
    overall_score: Optional[float] = None
