"""
Script processing schemas.
"""
from typing import List, Optional, Dict, Any
from pydantic import BaseModel, Field, ConfigDict


class ScriptProcessRequest(BaseModel):
    project_id: str
    novel_text: str = Field(..., description="Original novel text")
    style_id: Optional[str] = Field("default", description="Style ID for script adaptation")
    provider: Optional[str] = None
    model: Optional[str] = None
    max_input_tokens: Optional[int] = Field(None, description="Max input tokens for LLM")
    skip_storyboard: Optional[bool] = Field(False, description="Whether to skip storyboard generation")
    extra_params: Optional[Dict[str, Any]] = Field(None, alias="extraParams")


class ScriptScene(BaseModel):
    scene_id: int
    location: str
    time: str
    action: str
    dialogue: str
    camera_movement: Optional[str]
    characters: List[str]


class NovelSummaryRequest(BaseModel):
    novel_text: str = Field(..., description="The novel text to summarize")
    project_id: Optional[str] = None
    provider: Optional[str] = None
    model: Optional[str] = None
    language: Optional[str] = Field("Chinese", description="Target language for summary")
    extra_params: Optional[Dict[str, Any]] = Field(None, alias="extraParams")
    global_summary: Optional[str] = Field(None, description="Global summary for context")


class CharacterProfile(BaseModel):
    name: str
    desc: Optional[str] = Field(None, description="Brief description or role")
    age: Optional[str] = None
    gender: Optional[str] = None
    role: Optional[str] = None
    appearance: Optional[str] = None
    tags: List[str] = []


class NovelSummaryResponse(BaseModel):
    summary: str
    title: Optional[str] = None


class CharacterExtractionRequest(BaseModel):
    novel_text: str = Field(..., description="Novel text to extract characters from")
    language: Optional[str] = Field("Chinese", description="Target language")
    project_id: Optional[str] = None
    provider: Optional[str] = None
    model: Optional[str] = None
    extra_params: Optional[Dict[str, Any]] = Field(None, alias="extraParams")
    global_summary: Optional[str] = Field(None, description="Global summary for context")
    known_characters: Optional[List[Dict[str, Any]]] = Field(None, description="Already known characters")


class CharacterExtractionResponse(BaseModel):
    characters: List[CharacterProfile] = []


class SceneProfile(BaseModel):
    name: str
    desc: str
    location: Optional[str] = None
    time_of_day: Optional[str] = Field(None, alias="timeOfDay")
    atmosphere: Optional[str] = None
    tags: List[str] = []


class SceneExtractionRequest(BaseModel):
    novel_text: str = Field(..., description="Novel text to extract scenes from")
    language: Optional[str] = Field("Chinese", description="Target language")
    project_id: Optional[str] = None
    provider: Optional[str] = None
    model: Optional[str] = None
    extra_params: Optional[Dict[str, Any]] = Field(None, alias="extraParams")
    global_summary: Optional[str] = Field(None, description="Global summary for context")
    known_scenes: Optional[List[Dict[str, Any]]] = Field(None, description="Already known scenes")


class SceneExtractionResponse(BaseModel):
    scenes: List[SceneProfile] = []


class PropProfile(BaseModel):
    name: str
    desc: str
    usage: Optional[str] = None
    tags: List[str] = []


class PropExtractionRequest(BaseModel):
    novel_text: str = Field(..., description="Novel text to extract props from")
    language: Optional[str] = Field("Chinese", description="Target language")
    project_id: Optional[str] = None
    provider: Optional[str] = None
    model: Optional[str] = None
    extra_params: Optional[Dict[str, Any]] = Field(None, alias="extraParams")
    global_summary: Optional[str] = Field(None, description="Global summary for context")
    known_props: Optional[List[Dict[str, Any]]] = Field(None, description="Already known props")


class PropExtractionResponse(BaseModel):
    props: List[PropProfile] = []


class StoryboardSplitItem(BaseModel):
    shot_number: int
    shot_title: str = Field(..., description="Short title or summary of the shot")
    visual_description: str = Field(..., description="Detailed visual description")
    dialogue: Optional[str] = None
    duration: Optional[str] = None
    shot_type: Optional[str] = None
    camera_movement: Optional[str] = None
    transition: Optional[str] = None
    audio_description: Optional[str] = None
    character_list: List[str] = []
    location: Optional[str] = None
    time_of_day: Optional[str] = None
    original_text: Optional[str] = Field(None, description="Original novel text")
    merge_image_prompt: Optional[str] = Field(None, description="Integrated visual prompt")
    video_prompt: Optional[str] = Field(None, description="Motion and action prompt")


class StoryboardSplitRequest(BaseModel):
    novel_text: str = Field(..., description="Novel text (full or chapter)")
    project_id: Optional[str] = None
    model: Optional[str] = None
    language: Optional[str] = Field("Chinese", description="Target language")
    known_characters: Optional[List[Dict[str, Any]]] = None
    known_scenes: Optional[List[Dict[str, Any]]] = None
    known_props: Optional[List[Dict[str, Any]]] = None
    extra_params: Optional[Dict[str, Any]] = Field(None, alias="extraParams")


class StoryboardSplitResponse(BaseModel):
    storyboards: List[StoryboardSplitItem]


class Chapter(BaseModel):
    title: str
    content: str
    word_count: int


class ChapterSplitRequest(BaseModel):
    novel_text: str = Field(..., description="Full novel text")
    regex_pattern: Optional[str] = Field(None, description="Custom regex pattern for splitting")


class ChapterSplitResponse(BaseModel):
    chapters: List[Chapter]
    total_chapters: int


class ScriptResponse(BaseModel):
    project_id: str
    title: str
    summary: Optional[str] = None
    characters: List[CharacterProfile] = []
    scenes: List[SceneProfile] = []
    props: List[PropProfile] = []
    chapters: List[Chapter] = []
    # Legacy fields
    script_scenes: Optional[List[ScriptScene]] = Field(None, alias="scriptScenes")
    character_list: Optional[List[str]] = Field(None, alias="characterList")


class PromptOptimizationRequest(BaseModel):
    prompt: str = Field(..., description="Original prompt to optimize")
    target_type: str = Field("image", description="Target generation type")
    template: Optional[str] = Field("general", description="Optimization template name")
    model: Optional[str] = None
    provider: Optional[str] = None
    language: Optional[str] = Field("Chinese", description="Target language")
    extra_params: Optional[Dict[str, Any]] = Field(None, alias="extraParams")


class PromptOptimizationResponse(BaseModel):
    original_prompt: str
    optimized_prompt: str
    target_type: str


class StyleRecommendationRequest(BaseModel):
    novel_text: str = Field(..., description="The novel text to analyze")
    project_id: Optional[str] = None
    provider: Optional[str] = None
    model: Optional[str] = None
    extra_params: Optional[Dict[str, Any]] = Field(None, alias="extraParams")


class StyleRecommendationResponse(BaseModel):
    recommended_style: str
    reasoning: str
