#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
API Request Models
API 请求数据模型
"""

from typing import List, Dict, Any, Optional, Union
from pydantic import BaseModel, Field, validator
from pathlib import Path


class TopicGenerationRequest(BaseModel):
    """主题生成请求"""
    creative_materials: str = Field(..., description="创意素材")
    num_topics: int = Field(default=5, ge=1, le=20, description="生成主题数量")
    month: Optional[str] = Field(default=None, description="月份限制")
    style: str = Field(default="活泼有趣", description="内容风格")
    
    class Config:
        schema_extra = {
            "example": {
                "creative_materials": "春节期间的三亚旅游，包含海滩、美食、住宿等内容",
                "num_topics": 5,
                "month": "2024年2月",
                "style": "活泼有趣"
            }
        }


class ContentGenerationRequest(BaseModel):
    """内容生成请求"""
    scenic_info: str = Field(..., description="景区信息")
    product_info: str = Field(..., description="产品信息")
    additional_requirements: Optional[str] = Field(default="", description="额外要求")
    style: str = Field(default="活泼有趣", description="内容风格")
    target_audience: str = Field(default="年轻旅行者", description="目标受众")
    
    class Config:
        schema_extra = {
            "example": {
                "scenic_info": "三亚亚龙湾，拥有7公里长的银白色海滩，被誉为天下第一湾",
                "product_info": "亚龙湾度假套餐，包含豪华酒店住宿、海滩活动、特色餐饮",
                "additional_requirements": "突出性价比和家庭友好性",
                "style": "活泼有趣",
                "target_audience": "年轻家庭"
            }
        }


class ContentJudgingRequest(BaseModel):
    """内容评判请求"""
    product_info: str = Field(..., description="产品信息")
    content_to_judge: str = Field(..., description="待评判内容")
    
    class Config:
        schema_extra = {
            "example": {
                "product_info": "三亚亚龙湾度假套餐，价格2999元，包含3天2晚住宿",
                "content_to_judge": "三亚超值游，只要1999元！包含所有费用，豪华体验等你来！"
            }
        }


class PosterGenerationRequest(BaseModel):
    """海报生成请求"""
    template_name: str = Field(default="vibrant", description="模板名称")
    content: Optional[Dict[str, Any]] = Field(default=None, description="海报内容")
    images: Optional[List[str]] = Field(default=None, description="图片路径列表")
    style_options: Optional[Dict[str, Any]] = Field(default=None, description="样式选项")
    
    # 生成内容的参数（当content为空时使用）
    scenic_info: Optional[str] = Field(default=None, description="景区信息")
    product_info: Optional[str] = Field(default=None, description="产品信息") 
    tweet_info: Optional[str] = Field(default="", description="推文信息")
    
    class Config:
        schema_extra = {
            "example": {
                "template_name": "vibrant",
                "scenic_info": "正佳极地海洋世界",
                "product_info": "夜场票，价格199元",
                "tweet_info": "520特惠活动",
                "style_options": {
                    "color_theme": "vibrant",
                    "transparent_background": True
                }
            }
        }


class DocumentProcessingRequest(BaseModel):
    """文档处理请求"""
    action: str = Field(..., description="处理动作", regex="^(extract|integrate|transform)$")
    document_paths: List[str] = Field(..., description="文档路径列表")
    format_type: Optional[str] = Field(default=None, description="转换格式类型")
    additional_requirements: Optional[str] = Field(default="", description="额外要求")
    
    @validator('document_paths')
    def validate_document_paths(cls, v):
        for path in v:
            if not Path(path).exists():
                raise ValueError(f"文档路径不存在: {path}")
        return v
    
    class Config:
        schema_extra = {
            "example": {
                "action": "transform",
                "document_paths": [
                    "documents/travel_guide.pdf",
                    "documents/hotel_info.docx"
                ],
                "format_type": "travel_guide",
                "additional_requirements": "突出实用信息和联系方式"
            }
        }


class XHSSearchRequest(BaseModel):
    """小红书搜索请求"""
    keyword: str = Field(..., description="搜索关键词")
    max_notes: int = Field(default=20, ge=1, le=100, description="最大笔记数量")
    sort_type: int = Field(default=0, ge=0, le=4, description="排序类型")
    note_type: int = Field(default=0, ge=0, le=2, description="笔记类型")
    cookies: Optional[str] = Field(default=None, description="Cookie字符串")
    
    class Config:
        schema_extra = {
            "example": {
                "keyword": "三亚旅游攻略",
                "max_notes": 20,
                "sort_type": 2,
                "note_type": 0,
                "cookies": "your_cookie_string_here"
            }
        }


class KeywordAnalysisRequest(BaseModel):
    """关键词分析请求"""
    content: str = Field(..., description="待分析内容")
    source_type: str = Field(default="general", description="内容源类型")
    enable_ai_analysis: bool = Field(default=True, description="是否启用AI分析")
    
    class Config:
        schema_extra = {
            "example": {
                "content": "三亚亚龙湾海滩度假攻略，包含住宿、美食、交通指南",
                "source_type": "travel_guide",
                "enable_ai_analysis": True
            }
        }


class ContentAnalysisRequest(BaseModel):
    """内容分析请求"""
    search_result: Optional[Dict[str, Any]] = Field(default=None, description="搜索结果")
    notes: Optional[List[Dict[str, Any]]] = Field(default=None, description="笔记列表")
    
    @validator('notes', 'search_result')
    def validate_input(cls, v, values):
        # 至少需要提供一个
        if not v and not values.get('search_result') and not values.get('notes'):
            raise ValueError("必须提供search_result或notes中的至少一个")
        return v
    
    class Config:
        schema_extra = {
            "example": {
                "notes": [
                    {
                        "note_id": "123",
                        "title": "三亚旅游攻略",
                        "content": "详细的旅游内容...",
                        "author": "旅游达人",
                        "likes": 1000,
                        "comments": 50
                    }
                ]
            }
        } 


class FileUploadRequest(BaseModel):
    """文件上传请求"""
    description: Optional[str] = Field(default="", description="文件描述")
    process_immediately: bool = Field(default=True, description="是否立即处理")
    
    class Config:
        schema_extra = {
            "example": {
                "description": "旅游攻略文档",
                "process_immediately": True
            }
        }


class PipelineRequest(BaseModel):
    """完整流水线请求"""
    creative_materials: str = Field(..., description="创意素材")
    num_topics: int = Field(default=5, ge=1, le=20, description="生成主题数量")
    scenic_info: str = Field(..., description="景区信息")
    product_info: str = Field(..., description="产品信息")
    style: str = Field(default="活泼有趣", description="内容风格")
    target_audience: str = Field(default="年轻旅行者", description="目标受众")
    generate_poster: bool = Field(default=False, description="是否生成海报")
    poster_template: str = Field(default="vibrant", description="海报模板")
    
    class Config:
        schema_extra = {
            "example": {
                "creative_materials": "春节期间的三亚旅游",
                "num_topics": 3,
                "scenic_info": "三亚亚龙湾",
                "product_info": "度假套餐，价格2999元",
                "style": "活泼有趣",
                "target_audience": "年轻家庭",
                "generate_poster": True,
                "poster_template": "vibrant"
            }
        }


class CookieManagementRequest(BaseModel):
    """Cookie管理请求"""
    name: str = Field(..., description="Cookie名称")
    cookie_string: str = Field(..., description="Cookie字符串")
    description: Optional[str] = Field(default="", description="Cookie描述")
    
    class Config:
        schema_extra = {
            "example": {
                "name": "xhs_main",
                "cookie_string": "a1=xxx; web_session=xxx;",
                "description": "主要的小红书Cookie"
            }
        }


class PromptBuildRequest(BaseModel):
    """提示词构建请求"""
    task_type: str = Field(..., description="任务类型")
    scenic_info: Optional[str] = Field(default="", description="景区信息")
    product_info: Optional[str] = Field(default="", description="产品信息")
    style: Optional[str] = Field(default="", description="风格要求")
    target_audience: Optional[str] = Field(default="", description="目标受众")
    custom_requirements: Optional[str] = Field(default="", description="自定义要求")
    
    class Config:
        schema_extra = {
            "example": {
                "task_type": "content_generation",
                "scenic_info": "三亚亚龙湾",
                "product_info": "度假套餐",
                "style": "活泼有趣",
                "target_audience": "年轻家庭",
                "custom_requirements": "突出性价比"
            }
        } 