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

"""
API Response Models  
API 响应数据模型
"""

from typing import List, Dict, Any, Optional, Union
from pydantic import BaseModel, Field
from datetime import datetime


class ApiResponse(BaseModel):
    """通用API响应"""
    success: bool = Field(..., description="是否成功")
    message: str = Field(default="", description="响应消息")
    data: Optional[Any] = Field(default=None, description="响应数据")
    error: Optional[str] = Field(default=None, description="错误信息")
    request_id: Optional[str] = Field(default=None, description="请求ID")
    timestamp: datetime = Field(default_factory=datetime.now, description="响应时间")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "操作成功",
                "data": {"result": "example_data"},
                "error": None,
                "request_id": "req_123456",
                "timestamp": "2024-01-01T12:00:00"
            }
        }


class TopicGenerationResponse(ApiResponse):
    """主题生成响应"""
    data: Optional[Dict[str, Any]] = Field(default=None, description="生成的主题数据")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "主题生成成功",
                "data": {
                    "topics": [
                        {
                            "index": 1,
                            "date": "2024-02-01",
                            "logic": "春节期间家庭出游需求旺盛",
                            "object": "三亚亚龙湾",
                            "product": "家庭度假套餐",
                            "style": "温馨家庭",
                            "targetAudience": "年轻家庭"
                        }
                    ],
                    "generation_stats": {
                        "total_generated": 5,
                        "processing_time": 2.5
                    }
                }
            }
        }


class ContentGenerationResponse(ApiResponse):
    """内容生成响应"""
    data: Optional[Dict[str, Any]] = Field(default=None, description="生成的内容数据")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "内容生成成功",
                "data": {
                    "content": {
                        "title": "三亚亚龙湾| 天下第一湾的绝美体验",
                        "content": "详细的旅游内容描述...",
                        "tag": ["三亚", "亚龙湾", "海滩度假"]
                    },
                    "generation_stats": {
                        "word_count": 500,
                        "processing_time": 3.2
                    }
                }
            }
        }


class ContentJudgingResponse(ApiResponse):
    """内容评判响应"""
    data: Optional[Dict[str, Any]] = Field(default=None, description="评判结果数据")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "内容评判完成",
                "data": {
                    "is_passed": False,
                    "confidence": 0.85,
                    "analysis": "价格信息与产品资料不符，存在虚假宣传风险",
                    "suggested_revisions": "建议修改价格为2999元，并添加具体包含项目"
                }
            }
        }


class PosterGenerationResponse(ApiResponse):
    """海报生成响应"""
    data: Optional[Dict[str, Any]] = Field(default=None, description="海报生成数据")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "海报生成成功",
                "data": {
                    "poster_path": "output/posters/vibrant_poster_20240101.png",
                    "fabric_json": "output/json/fabric_data_20240101.json",
                    "content": {
                        "title": "正佳极地海洋世界",
                        "slogan": "夜游海洋，梦幻之旅",
                        "price": "199",
                        "ticket_type": "夜场票"
                    },
                    "template_info": {
                        "name": "vibrant",
                        "size": [900, 1200],
                        "transparent_background": True
                    }
                }
            }
        }


class DocumentProcessingResponse(ApiResponse):
    """文档处理响应"""
    data: Optional[Dict[str, Any]] = Field(default=None, description="文档处理数据")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "文档处理完成",
                "data": {
                    "processed_documents": 2,
                    "output_files": [
                        "output/documents/integrated_content.txt",
                        "output/documents/travel_guide_transformed.txt"
                    ],
                    "statistics": {
                        "total_characters": 50000,
                        "key_topics": ["住宿", "交通", "美食", "景点"],
                        "processing_time": 15.3
                    }
                }
            }
        }


class XHSSearchResponse(ApiResponse):
    """小红书搜索响应"""
    data: Optional[Dict[str, Any]] = Field(default=None, description="搜索结果数据")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "搜索完成",
                "data": {
                    "keyword": "三亚旅游攻略",
                    "notes": [
                        {
                            "note_id": "123abc",
                            "title": "三亚7天6晚详细攻略",
                            "content": "超详细的三亚旅游攻略...",
                            "author": "旅游达人小明",
                            "likes": 1500,
                            "comments": 89,
                            "tags": ["三亚", "攻略", "海滩"]
                        }
                    ],
                    "total_count": 20,
                    "search_time": "2024-01-01T12:00:00"
                }
            }
        }


class KeywordAnalysisResponse(ApiResponse):
    """关键词分析响应"""
    data: Optional[Dict[str, Any]] = Field(default=None, description="关键词分析数据")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "关键词分析完成",
                "data": {
                    "keywords": ["三亚", "亚龙湾", "海滩", "度假", "攻略"],
                    "keyword_scores": {
                        "三亚": 85.5,
                        "亚龙湾": 78.2,
                        "海滩": 65.8
                    },
                    "categories": {
                        "地理位置": ["三亚", "亚龙湾"],
                        "景点类型": ["海滩"],
                        "活动体验": ["度假"]
                    },
                    "search_suggestions": [
                        "三亚攻略",
                        "亚龙湾住宿",
                        "三亚美食"
                    ]
                }
            }
        }


class ContentAnalysisResponse(ApiResponse):
    """内容分析响应"""
    data: Optional[Dict[str, Any]] = Field(default=None, description="内容分析数据")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "内容分析完成",
                "data": {
                    "content_quality_score": 78.5,
                    "engagement_score": 65.2,
                    "content_themes": ["旅游攻略", "美食体验", "住宿推荐"],
                    "sentiment_analysis": {
                        "positive": 0.7,
                        "negative": 0.1,
                        "neutral": 0.2,
                        "overall": "positive"
                    },
                    "recommendations": [
                        "内容质量较好，建议增加更多实用信息",
                        "互动数据中等，可关注更热门的内容"
                    ]
                }
            }
        } 


class FileUploadResponse(ApiResponse):
    """文件上传响应"""
    data: Optional[Dict[str, Any]] = Field(default=None, description="上传结果数据")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "文件上传成功",
                "data": {
                    "file_id": "upload_123456",
                    "file_path": "uploads/document_20240101.pdf",
                    "file_size": 1024000,
                    "processing_status": "completed"
                }
            }
        }


class PipelineResponse(ApiResponse):
    """完整流水线响应"""
    data: Optional[Dict[str, Any]] = Field(default=None, description="流水线执行结果")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "流水线执行完成",
                "data": {
                    "topics": [{"index": 1, "title": "三亚亲子游攻略"}],
                    "content": {"title": "三亚亲子游", "content": "详细内容..."},
                    "judge_result": {"is_passed": True, "confidence": 0.9},
                    "poster": {"poster_path": "output/poster.png"},
                    "execution_time": 30.5
                }
            }
        }


class CookieManagementResponse(ApiResponse):
    """Cookie管理响应"""
    data: Optional[Dict[str, Any]] = Field(default=None, description="Cookie管理结果")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "Cookie管理操作成功",
                "data": {
                    "total_cookies": 3,
                    "valid_cookies": 2,
                    "current_cookie": "xhs_main",
                    "operation": "add"
                }
            }
        }


class PromptBuildResponse(ApiResponse):
    """提示词构建响应"""
    data: Optional[Dict[str, Any]] = Field(default=None, description="构建的提示词")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "提示词构建成功",
                "data": {
                    "system_prompt": "你是一位专业的旅游内容创作者...",
                    "user_prompt": "请为三亚亚龙湾创作内容...",
                    "prompt_template": "content_generation",
                    "variables": {"scenic_info": "三亚亚龙湾"}
                }
            }
        }


class TaskManagementResponse(ApiResponse):
    """任务管理响应"""
    data: Optional[Dict[str, Any]] = Field(default=None, description="任务管理数据")
    
    class Config:
        schema_extra = {
            "example": {
                "success": True,
                "message": "任务查询成功",
                "data": {
                    "task_id": "task_123456",
                    "status": "completed",
                    "created_at": "2024-01-01T12:00:00",
                    "completed_at": "2024-01-01T12:05:00",
                    "result_files": ["output/result.json"],
                    "statistics": {"processing_time": 300}
                }
            }
        } 