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

"""
Default Configuration Provider
默认配置提供器 - 提供算法包的默认配置，支持动态提示词加载
"""

from typing import Dict, Any
import os
from pathlib import Path


class DefaultConfigProvider:
    """默认配置提供器"""
    
    @staticmethod
    def get_ai_model_config() -> Dict[str, Any]:
        """获取AI模型默认配置 - 支持任务级别参数"""
        return {
            "model": "qwq-plus",
            "api_url": os.getenv("AI_API_URL", ""),
            "api_key": os.getenv("AI_API_KEY", ""),
            "temperature": 0.7,
            "top_p": 0.5,
            "presence_penalty": 1.2,
            "frequency_penalty": 0.0,
            "max_tokens": None,
            "timeout": 60,
            "max_retries": 3,
            "enable_stream": True,
            "stream_chunk_size": 1024,
            
            # 任务级别的模型参数配置
            "task_configs": {
                "topic_generation": {
                    "temperature": 0.8,  # 主题生成需要更高创造性
                    "top_p": 0.9,
                    "presence_penalty": 1.0
                },
                "content_generation": {
                    "temperature": 0.7,  # 内容生成平衡创造性和准确性
                    "top_p": 0.8,
                    "presence_penalty": 1.2
                },
                "content_judging": {
                    "temperature": 0.3,  # 内容审核需要更严格和一致
                    "top_p": 0.5,
                    "presence_penalty": 0.5
                },
                "poster_generation": {
                    "temperature": 0.6,  # 海报文案生成
                    "top_p": 0.7,
                    "presence_penalty": 1.0
                }
            }
        }
    
    @staticmethod
    def get_prompt_config() -> Dict[str, Any]:
        """获取提示词默认配置 - 支持动态加载"""
        return {
            # 提示词模板目录配置
            "template_directory": "templates/prompts",
            "enable_template_loading": True,
            
            # 提示词文件映射
            "template_files": {
                "topic_generation": {
                    "system": "generateTopics/system.txt",
                    "user": "generateTopics/user.txt"
                },
                "content_generation": {
                    "system": "generateContent/system.txt", 
                    "user": "generateContent/user.txt"
                },
                "content_judging": {
                    "system": "judgeContent/system.txt",
                    "user": "judgeContent/user.txt"
                },
                "poster_generation": {
                    "system": "generatePoster/system.txt",
                    "user": "generatePoster/user.txt"
                }
            },
            
            # 回退的默认提示词（当文件加载失败时使用）
            "fallback_prompts": {
                "topic_generation": {
                    "system": "你是一个专业的旅游选题策划师。",
                    "user": "请生成{numTopics}个旅游主题。日期：{month}"
                },
                "content_generation": {
                    "system": "你是一个专业的旅游文案写手。",
                    "user": "请根据以下主题生成旅游内容：{topic_info}"
                },
                "content_judging": {
                    "system": "你是一个专业的内容审核专家。",
                    "user": "请审核以下内容：{content}"
                }
            }
        }
    
    @staticmethod
    def get_content_generation_config() -> Dict[str, Any]:
        """获取内容生成默认配置"""
        return {
            "topic_count": 5,
            "topic_date_range": None,
            "topic_style": "default",
            "content_length": "medium",
            "content_style": "default", 
            "enable_auto_judge": True,
            "judge_enabled": True,
            "judge_threshold": 0.7,
            "refer_sampling_rate": 0.5,
            "enable_refer_content": True,
            
            # JSON修复配置
            "enable_json_repair": True,
            "json_repair_attempts": 3,
            
            # 结果字段映射配置
            "result_field_mapping": {
                "topic_generation": {
                    "expected_fields": ["index", "date", "logic", "object", "product", "productLogic", "style", "styleLogic", "targetAudience", "targetAudienceLogic"],
                    "required_fields": ["index", "date", "logic", "object"]
                },
                "content_generation": {
                    "expected_fields": ["title", "content", "tag"],
                    "required_fields": ["title", "content"]
                },
                "content_judging": {
                    "expected_fields": ["analysis", "title", "content"],
                    "required_fields": ["title", "content"]
                }
            }
        }
    
    @staticmethod
    def get_poster_generation_config() -> Dict[str, Any]:
        """获取海报生成默认配置 - 从硬编码中提取"""
        return {
            "default_size": [900, 1200],
            "supported_sizes": [
                [900, 1200],   # 竖屏海报
                [1080, 1080], # 方形海报
                [750, 1334]   # 手机屏幕
            ],
            "default_template": "vibrant",
            "available_templates": ["vibrant", "business", "collage"],
            "template_selection_mode": "manual",
            
            # 字体配置
            "font_configs": {
                "chinese_bold": {
                    "family": "兰亭粗黑简",
                    "fallback": ["SimHei", "Arial"]
                },
                "default": {
                    "family": "Arial",
                    "fallback": ["Times New Roman"]
                }
            },
            "default_font_family": "chinese_bold",
            
            # 颜色主题 - 从VibrantTemplate中提取
            "color_themes": {
                "ocean_deep": {
                    "primary": [[0, 30, 80], [20, 120, 220]],
                    "secondary": [[10, 50, 150], [40, 140, 240]]
                },
                "sunset_warm": {
                    "primary": [[255, 94, 77], [255, 154, 0]],
                    "secondary": [[240, 80, 60], [240, 140, 20]]
                },
                "cool_mint": {
                    "primary": [[64, 224, 208], [127, 255, 212]],
                    "secondary": [[80, 200, 190], [100, 230, 200]]
                },
                "royal_purple": {
                    "primary": [[75, 0, 130], [138, 43, 226]],
                    "secondary": [[90, 20, 150], [120, 60, 200]]
                },
                "forest_green": {
                    "primary": [[34, 139, 34], [144, 238, 144]],
                    "secondary": [[50, 120, 50], [120, 220, 120]]
                },
                "fire_red": {
                    "primary": [[220, 20, 60], [255, 69, 0]],
                    "secondary": [[200, 40, 80], [230, 90, 30]]
                }
            },
            "default_color_theme": "ocean_deep",
            
            # 毛玻璃效果配置 - 从VibrantTemplate中提取
            "glass_effect": {
                "max_opacity": 240,
                "blur_radius": 22,
                "transition_height": 80,
                "intensity_multiplier": 1.5
            },
            
            # 文本效果配置
            "text_effects": {
                "shadow": True,
                "shadow_offset": [2, 2],
                "shadow_color": [0, 0, 0, 128],
                "outline": False,
                "outline_width": 1,
                "outline_color": [255, 255, 255]
            },
            
            # 输出配置
            "output_format": "PNG",
            "output_quality": 95,
            "generate_thumbnail": True,
            "thumbnail_size": [300, 400]
        }
    
    @staticmethod 
    def get_document_processing_config() -> Dict[str, Any]:
        """获取文档处理默认配置"""
        return {
            "supported_extensions": [".txt", ".md", ".pdf", ".docx", ".doc", ".xlsx", ".xls"],
            "max_file_size": 50 * 1024 * 1024,  # 50MB
            "encoding": "utf-8",
            "extract_images": True,
            "extract_tables": True,
            "enable_content_cleaning": True,
            "remove_extra_whitespace": True,
            "normalize_text": True,
            "enable_web_scraping": True,
            "scraping_config": {
                "max_notes": 20,
                "sort_type": 0,
                "note_type": 0,
                "download_media": True,
                "timeout": 30
            }
        }
    
    @staticmethod
    def get_output_config() -> Dict[str, Any]:
        """获取输出配置 - 解决硬编码的文件名问题"""
        return {
            "base_output_directory": "result",
            "enable_timestamped_folders": True,
            "folder_name_pattern": "run_{timestamp}",
            
            # 文件命名模式 - 替代硬编码的文件名
            "file_naming": {
                "topics": "topics.json",
                "content": "article.json",
                "poster": "poster.png",
                "raw_response": "{stage}_raw_response.txt",
                "system_prompt": "{stage}_system_prompt.txt", 
                "user_prompt": "{stage}_user_prompt.txt",
                "judged_content": "article_judged.json",
                "metadata": "metadata.json"
            },
            
            "save_raw_responses": True,
            "save_prompts": True,
            "save_metadata": True,
            "enable_compression": False,
            "backup_count": 5
        }
    
    @staticmethod
    def get_resource_config() -> Dict[str, Any]:
        """获取资源配置 - 包括static文件管理"""
        return {
            "resource_base_directory": None,  # 由调用方设置
            "font_directory": "assets/fonts",
            
            # 字体文件映射 - 解决硬编码字体路径
            "font_files": {
                "chinese_bold": "兰亭粗黑简.TTF",
                "default": "Arial.ttf"
            },
            
            "prompt_template_directory": "templates/prompts",
            "image_assets_directory": "assets/images",
            "default_background_images": [],
            
            # 静态文件配置 - 解决爬虫模块依赖
            "static_files_directory": "static",
            "required_js_files": {
                "xhs_xs_xsc_56": "xhs_xs_xsc_56.js",
                "xhs_xray": "xhs_xray.js", 
                "xhs_creator_xs": "xhs_creator_xs.js",
                "xhs_xray_pack1": "xhs_xray_pack1.js",
                "xhs_xray_pack2": "xhs_xray_pack2.js"
            }
        }
    
    @staticmethod
    def get_web_crawling_config() -> Dict[str, Any]:
        """获取网页爬虫默认配置"""
        return {
            "request_interval": 1.0,
            "max_requests": 100,
            "request_timeout": 30.0,
            "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
            "enable_proxy": False,
            "proxy_config": None,
            "max_retries": 3,
            "retry_delay": 2.0,
            "min_content_length": 50,
            "max_content_length": 10000,
            "xhs_search_config": {
                "default_sort_type": 0,
                "default_note_type": 0,
                "enable_image_download": True,
                "enable_video_download": False,
                "search_result_limit": 50
            }
        }
    
    @staticmethod
    def get_keyword_analysis_config() -> Dict[str, Any]:
        """获取关键词分析默认配置"""
        return {
            "max_keywords": 20,
            "min_keyword_length": 2,
            "max_keyword_length": 10,
            "enable_ai_analysis": True,
            "ai_analysis_threshold": 100,
            "frequency_weight": 0.4,
            "position_weight": 0.3,
            "type_weight": 0.3,
            "enable_categorization": True,
            "category_threshold": 2,
            "max_suggestions": 20,
            "enable_combination_suggestions": True
        }
    
    @staticmethod
    def get_content_analysis_config() -> Dict[str, Any]:
        """获取内容分析默认配置"""
        return {
            "content_length_weight": 0.25,
            "title_quality_weight": 0.20,
            "media_richness_weight": 0.20,
            "structure_weight": 0.20,
            "tag_completeness_weight": 0.15,
            "likes_weight": 0.40,
            "comments_weight": 0.35,
            "shares_weight": 0.25,
            "enable_sentiment_analysis": True,
            "enable_theme_extraction": True,
            "enable_readability_analysis": True,
            "max_themes": 10,
            "theme_frequency_threshold": 2
        }
    
    @classmethod
    def get_complete_default_config(cls) -> Dict[str, Any]:
        """获取完整的默认配置"""
        return {
            "ai_model": cls.get_ai_model_config(),
            "prompts": cls.get_prompt_config(),
            "content_generation": cls.get_content_generation_config(),
            "poster_generation": cls.get_poster_generation_config(),
            "document_processing": cls.get_document_processing_config(),
            "output": cls.get_output_config(),
            "resources": cls.get_resource_config()
        } 