"""
剧集内容数据模型

用于存储生成后的剧集内容，支持预览、编辑和导出
"""
from pydantic import BaseModel, Field
from typing import Optional, Dict, Any
from datetime import datetime
from enum import Enum


class ContentStatus(str, Enum):
    """内容状态"""
    DRAFT = "draft"           # 草稿
    GENERATING = "generating" # 生成中
    PENDING_REVIEW = "pending_review"  # 待审核
    APPROVED = "approved"     # 已通过
    REJECTED = "rejected"     # 已拒绝


class EpisodeContent(BaseModel):
    """单集内容"""
    id: str = Field(..., description="内容 ID")
    project_id: str = Field(..., description="项目 ID")
    episode_number: int = Field(..., description="集数", ge=1)
    title: Optional[str] = Field(None, description="标题")
    content: str = Field(..., description="内容（Markdown 格式）")
    status: ContentStatus = Field(default=ContentStatus.DRAFT, description="状态")
    quality_score: Optional[float] = Field(None, description="质量分数")
    review_issues: list = Field(default_factory=list, description="审核问题")

    # 元数据
    created_at: datetime = Field(default_factory=datetime.now, description="创建时间")
    updated_at: datetime = Field(default_factory=datetime.now, description="更新时间")
    created_by: str = Field(default="system", description="创建者")

    # 生成参数
    generation_params: Optional[Dict[str, Any]] = Field(None, description="生成参数")

    # 版本信息
    version: int = Field(default=1, description="版本号")
    parent_version: Optional[int] = Field(None, description="父版本号")

    class Config:
        json_schema_extra = {
            "example": {
                "id": "ep_001_001",
                "project_id": "proj_001",
                "episode_number": 1,
                "title": "第一集：初遇",
                "content": "# 第一集\n\n## 场景一\n...",
                "status": "draft",
                "quality_score": 85.5,
                "review_issues": []
            }
        }


class EpisodeContentCreate(BaseModel):
    """创建剧集内容"""
    project_id: str
    episode_number: int
    title: Optional[str] = None
    content: str
    generation_params: Optional[Dict[str, Any]] = None


class EpisodeContentUpdate(BaseModel):
    """更新剧集内容"""
    title: Optional[str] = None
    content: Optional[str] = None
    status: Optional[ContentStatus] = None


class EpisodeContentReview(BaseModel):
    """审核剧集内容"""
    approved: bool = Field(..., description="是否通过")
    comment: Optional[str] = Field(None, description="审核意见")


class ExportFormat(str, Enum):
    """导出格式"""
    MARKDOWN = "markdown"
    TXT = "txt"
    PDF = "pdf"
    DOCX = "docx"


class ExportRequest(BaseModel):
    """导出请求"""
    format: ExportFormat
    episode_numbers: Optional[list[int]] = Field(None, description="指定集数，空表示全部")
    include_metadata: bool = Field(default=False, description="是否包含元数据")
