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

"""
Content Analyzer
内容分析器 - 分析爬取的内容质量和特征
"""

import logging
import re
from typing import List, Dict, Any, Optional
from dataclasses import dataclass
from datetime import datetime
from collections import Counter

from .xhs_crawler import XHSNote, XHSSearchResult
from ..config import AlgorithmConfig
from ..core import AIService, PromptManager, JSONProcessor
from ..exceptions import ContentGenerationError

logger = logging.getLogger(__name__)


@dataclass
class AnalysisResult:
    """内容分析结果"""
    content_quality_score: float
    engagement_score: float
    content_themes: List[str]
    sentiment_analysis: Dict[str, Any]
    readability_score: float
    unique_features: List[str]
    content_statistics: Dict[str, Any]
    recommendations: List[str]
    analyzed_at: datetime
    
    def __post_init__(self):
        if self.analyzed_at is None:
            self.analyzed_at = datetime.now()
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return {
            'content_quality_score': self.content_quality_score,
            'engagement_score': self.engagement_score,
            'content_themes': self.content_themes,
            'sentiment_analysis': self.sentiment_analysis,
            'readability_score': self.readability_score,
            'unique_features': self.unique_features,
            'content_statistics': self.content_statistics,
            'recommendations': self.recommendations,
            'analyzed_at': self.analyzed_at.isoformat() if self.analyzed_at else None
        }


class ContentAnalyzer:
    """
    内容分析器
    分析爬取内容的质量、特征和价值
    """
    
    def __init__(self, config: AlgorithmConfig):
        """
        初始化内容分析器
        
        Args:
            config: 算法配置
        """
        self.config = config
        self.ai_service = AIService(config.ai_model)
        self.prompt_manager = PromptManager(config.prompts, config.resources)
        self.json_processor = JSONProcessor(
            enable_repair=config.content_generation.enable_json_repair,
            max_repair_attempts=config.content_generation.json_repair_attempts
        )
        
        # 获取任务特定的模型配置
        self.task_model_config = config.ai_model.get_task_config("content_analysis")
        
        logger.info("内容分析器初始化完成")
    
    def analyze_xhs_notes(self, notes: List[XHSNote]) -> AnalysisResult:
        """
        分析小红书笔记列表
        
        Args:
            notes: 小红书笔记列表
            
        Returns:
            AnalysisResult: 分析结果
        """
        try:
            logger.info(f"开始分析 {len(notes)} 条小红书笔记")
            
            if not notes:
                return self._create_empty_analysis()
            
            # 1. 内容质量评分
            quality_score = self._calculate_content_quality(notes)
            
            # 2. 互动数据评分
            engagement_score = self._calculate_engagement_score(notes)
            
            # 3. 内容主题分析
            themes = self._extract_content_themes(notes)
            
            # 4. 情感分析
            sentiment = self._analyze_sentiment(notes)
            
            # 5. 可读性评分
            readability = self._calculate_readability(notes)
            
            # 6. 独特特征识别
            features = self._identify_unique_features(notes)
            
            # 7. 统计信息
            statistics = self._generate_statistics(notes)
            
            # 8. 生成建议
            recommendations = self._generate_recommendations(notes, quality_score, engagement_score)
            
            result = AnalysisResult(
                content_quality_score=quality_score,
                engagement_score=engagement_score,
                content_themes=themes,
                sentiment_analysis=sentiment,
                readability_score=readability,
                unique_features=features,
                content_statistics=statistics,
                recommendations=recommendations,
                analyzed_at=datetime.now()
            )
            
            logger.info(f"内容分析完成，质量评分: {quality_score:.2f}")
            return result
            
        except Exception as e:
            error_msg = f"内容分析失败: {str(e)}"
            logger.error(error_msg, exc_info=True)
            raise ContentGenerationError(error_msg)
    
    def analyze_search_result(self, search_result: XHSSearchResult) -> AnalysisResult:
        """
        分析搜索结果
        
        Args:
            search_result: 搜索结果
            
        Returns:
            AnalysisResult: 分析结果
        """
        return self.analyze_xhs_notes(search_result.notes)
    
    def _create_empty_analysis(self) -> AnalysisResult:
        """创建空的分析结果"""
        return AnalysisResult(
            content_quality_score=0.0,
            engagement_score=0.0,
            content_themes=[],
            sentiment_analysis={'positive': 0, 'negative': 0, 'neutral': 1},
            readability_score=0.0,
            unique_features=[],
            content_statistics={'note_count': 0},
            recommendations=['没有可分析的内容'],
            analyzed_at=datetime.now()
        )
    
    def _calculate_content_quality(self, notes: List[XHSNote]) -> float:
        """计算内容质量评分（0-100）"""
        if not notes:
            return 0.0
        
        quality_scores = []
        
        for note in notes:
            score = 0.0
            
            # 1. 内容长度评分（0-25分）
            content_length = len(note.content)
            if content_length >= 200:
                score += 25
            elif content_length >= 100:
                score += 15
            elif content_length >= 50:
                score += 10
            else:
                score += 5
            
            # 2. 标题质量评分（0-20分）
            title_score = self._evaluate_title_quality(note.title)
            score += title_score
            
            # 3. 媒体丰富度评分（0-20分）
            media_score = 0
            if note.images:
                media_score += min(len(note.images) * 3, 15)
            if note.videos:
                media_score += 5
            score += min(media_score, 20)
            
            # 4. 标签完整性评分（0-15分）
            if note.tags:
                tag_score = min(len(note.tags) * 3, 15)
                score += tag_score
            
            # 5. 内容结构评分（0-20分）
            structure_score = self._evaluate_content_structure(note.content)
            score += structure_score
            
            quality_scores.append(score)
        
        return sum(quality_scores) / len(quality_scores)
    
    def _evaluate_title_quality(self, title: str) -> float:
        """评估标题质量"""
        score = 0.0
        
        # 长度适中（0-8分）
        length = len(title)
        if 10 <= length <= 30:
            score += 8
        elif 5 <= length <= 50:
            score += 5
        else:
            score += 2
        
        # 包含关键信息（0-6分）
        keywords = ['攻略', '推荐', '必去', '美食', '景点', '游玩', '体验']
        if any(kw in title for kw in keywords):
            score += 6
        
        # 吸引力（0-6分）
        attractive_words = ['超美', '绝美', '必打卡', '神仙', '小众', '宝藏', '惊艳']
        if any(word in title for word in attractive_words):
            score += 6
        
        return score
    
    def _evaluate_content_structure(self, content: str) -> float:
        """评估内容结构"""
        score = 0.0
        
        # 段落结构（0-8分）
        paragraphs = [p.strip() for p in content.split('\n') if p.strip()]
        if len(paragraphs) >= 3:
            score += 8
        elif len(paragraphs) >= 2:
            score += 5
        else:
            score += 2
        
        # 包含列表或要点（0-6分）
        if any(marker in content for marker in ['1.', '2.', '•', '·', '①', '②']):
            score += 6
        
        # 包含实用信息（0-6分）
        useful_info = ['地址', '时间', '价格', '门票', '交通', '电话']
        if any(info in content for info in useful_info):
            score += 6
        
        return score
    
    def _calculate_engagement_score(self, notes: List[XHSNote]) -> float:
        """计算互动数据评分（0-100）"""
        if not notes:
            return 0.0
        
        engagement_scores = []
        
        for note in notes:
            # 归一化互动数据
            likes_norm = min(note.likes / 1000, 1.0) * 40  # 点赞权重40%
            comments_norm = min(note.comments / 100, 1.0) * 35  # 评论权重35%
            shares_norm = min(note.shares / 50, 1.0) * 25  # 分享权重25%
            
            total_score = likes_norm + comments_norm + shares_norm
            engagement_scores.append(total_score)
        
        return sum(engagement_scores) / len(engagement_scores)
    
    def _extract_content_themes(self, notes: List[XHSNote]) -> List[str]:
        """提取内容主题"""
        # 合并所有内容进行主题分析
        all_content = " ".join([note.title + " " + note.content for note in notes])
        all_tags = []
        for note in notes:
            all_tags.extend(note.tags)
        
        # 基于高频词和标签提取主题
        themes = []
        
        # 1. 从标签中提取主题
        tag_counter = Counter(all_tags)
        top_tags = [tag for tag, count in tag_counter.most_common(10) if count >= 2]
        themes.extend(top_tags)
        
        # 2. 从内容中提取主题关键词
        travel_themes = {
            '美食体验': ['美食', '小吃', '餐厅', '特色菜', '当地菜'],
            '景点游览': ['景点', '景区', '公园', '博物馆', '寺庙', '古迹'],
            '住宿体验': ['酒店', '民宿', '住宿', '客栈', '度假村'],
            '交通出行': ['交通', '地铁', '公交', '打车', '自驾', '飞机'],
            '购物体验': ['购物', '商场', '特产', '纪念品', '市场'],
            '文化体验': ['文化', '历史', '传统', '习俗', '艺术'],
            '自然风光': ['自然', '风景', '山水', '海滨', '森林', '湖泊'],
            '娱乐活动': ['娱乐', '活动', '表演', '游戏', '运动']
        }
        
        for theme, keywords in travel_themes.items():
            if any(keyword in all_content for keyword in keywords):
                themes.append(theme)
        
        return list(set(themes))[:10]  # 去重并限制数量
    
    def _analyze_sentiment(self, notes: List[XHSNote]) -> Dict[str, Any]:
        """简单的情感分析"""
        positive_words = [
            '好', '棒', '美', '赞', '喜欢', '推荐', '值得', '满意', '惊艳', '完美',
            '舒服', '开心', '愉快', '享受', '感动', '震撼', '神仙', '绝美'
        ]
        
        negative_words = [
            '差', '烂', '坑', '贵', '不好', '失望', '糟糕', '难吃', '不值',
            '后悔', '踩雷', '避雷', '不推荐', '一般', '普通'
        ]
        
        positive_count = 0
        negative_count = 0
        total_words = 0
        
        for note in notes:
            content = note.title + " " + note.content
            total_words += len(content)
            
            for word in positive_words:
                positive_count += content.count(word)
            
            for word in negative_words:
                negative_count += content.count(word)
        
        total_sentiment = positive_count + negative_count
        
        if total_sentiment == 0:
            return {
                'positive': 0.5,
                'negative': 0.2,
                'neutral': 0.3,
                'overall': 'neutral'
            }
        
        positive_ratio = positive_count / total_sentiment
        negative_ratio = negative_count / total_sentiment
        neutral_ratio = max(0, 1 - positive_ratio - negative_ratio)
        
        # 判断整体情感倾向
        if positive_ratio > 0.6:
            overall = 'positive'
        elif negative_ratio > 0.4:
            overall = 'negative'
        else:
            overall = 'neutral'
        
        return {
            'positive': positive_ratio,
            'negative': negative_ratio,
            'neutral': neutral_ratio,
            'overall': overall,
            'positive_words_count': positive_count,
            'negative_words_count': negative_count
        }
    
    def _calculate_readability(self, notes: List[XHSNote]) -> float:
        """计算可读性评分"""
        if not notes:
            return 0.0
        
        readability_scores = []
        
        for note in notes:
            content = note.content
            score = 0.0
            
            # 1. 句子长度评分（0-30分）
            sentences = re.split(r'[。！？.!?]', content)
            valid_sentences = [s.strip() for s in sentences if s.strip()]
            
            if valid_sentences:
                avg_sentence_length = sum(len(s) for s in valid_sentences) / len(valid_sentences)
                if 10 <= avg_sentence_length <= 30:
                    score += 30
                elif 5 <= avg_sentence_length <= 50:
                    score += 20
                else:
                    score += 10
            
            # 2. 段落结构评分（0-25分）
            paragraphs = [p.strip() for p in content.split('\n') if p.strip()]
            if len(paragraphs) >= 3:
                score += 25
            elif len(paragraphs) >= 2:
                score += 15
            else:
                score += 5
            
            # 3. 标点符号使用评分（0-20分）
            punctuation_count = len(re.findall(r'[，。！？、；：]', content))
            text_length = len(content)
            if text_length > 0:
                punctuation_ratio = punctuation_count / text_length
                if 0.05 <= punctuation_ratio <= 0.15:
                    score += 20
                elif 0.02 <= punctuation_ratio <= 0.25:
                    score += 15
                else:
                    score += 10
            
            # 4. 信息层次评分（0-25分）
            if any(marker in content for marker in ['1.', '2.', '一、', '二、', '①', '②']):
                score += 15
            if any(marker in content for marker in ['地址：', '时间：', '门票：', '交通：']):
                score += 10
            
            readability_scores.append(score)
        
        return sum(readability_scores) / len(readability_scores)
    
    def _identify_unique_features(self, notes: List[XHSNote]) -> List[str]:
        """识别独特特征"""
        features = []
        
        # 1. 媒体特征
        total_images = sum(len(note.images) for note in notes)
        total_videos = sum(len(note.videos) for note in notes)
        
        if total_images / len(notes) > 5:
            features.append("图片丰富")
        if total_videos > 0:
            features.append("包含视频")
        
        # 2. 内容特征
        all_content = " ".join([note.content for note in notes])
        
        if "攻略" in all_content:
            features.append("攻略类内容")
        if any(word in all_content for word in ["测评", "体验", "真实"]):
            features.append("体验分享")
        if any(word in all_content for word in ["推荐", "必去", "值得"]):
            features.append("推荐类内容")
        if any(word in all_content for word in ["避雷", "踩坑", "注意"]):
            features.append("避坑提醒")
        
        # 3. 互动特征
        avg_engagement = sum(note.likes + note.comments + note.shares for note in notes) / len(notes)
        if avg_engagement > 1000:
            features.append("高互动内容")
        
        # 4. 时效性特征
        if any(word in all_content for word in ["最新", "2024", "新开"]):
            features.append("时效性强")
        
        return features
    
    def _generate_statistics(self, notes: List[XHSNote]) -> Dict[str, Any]:
        """生成统计信息"""
        if not notes:
            return {'note_count': 0}
        
        return {
            'note_count': len(notes),
            'total_likes': sum(note.likes for note in notes),
            'total_comments': sum(note.comments for note in notes),
            'total_shares': sum(note.shares for note in notes),
            'avg_likes': sum(note.likes for note in notes) / len(notes),
            'avg_comments': sum(note.comments for note in notes) / len(notes),
            'avg_shares': sum(note.shares for note in notes) / len(notes),
            'total_content_length': sum(len(note.content) for note in notes),
            'avg_content_length': sum(len(note.content) for note in notes) / len(notes),
            'unique_authors': len(set(note.author for note in notes)),
            'total_tags': sum(len(note.tags) for note in notes),
            'unique_tags': len(set(tag for note in notes for tag in note.tags)),
            'has_images': sum(1 for note in notes if note.images),
            'has_videos': sum(1 for note in notes if note.videos)
        }
    
    def _generate_recommendations(
        self, 
        notes: List[XHSNote], 
        quality_score: float, 
        engagement_score: float
    ) -> List[str]:
        """生成建议"""
        recommendations = []
        
        # 基于质量评分的建议
        if quality_score < 50:
            recommendations.append("建议寻找更高质量的内容源")
            recommendations.append("关注内容的完整性和结构性")
        elif quality_score > 80:
            recommendations.append("内容质量优秀，可作为参考模板")
        
        # 基于互动评分的建议
        if engagement_score < 30:
            recommendations.append("关注用户互动更活跃的内容")
        elif engagement_score > 70:
            recommendations.append("高互动内容，具有较强参考价值")
        
        # 基于内容特征的建议
        all_content = " ".join([note.content for note in notes])
        
        if "地址" not in all_content:
            recommendations.append("建议补充具体的地理位置信息")
        
        if "价格" not in all_content and "门票" not in all_content:
            recommendations.append("建议添加价格相关信息")
        
        if len(set(note.author for note in notes)) < len(notes) * 0.7:
            recommendations.append("建议增加内容源的多样性")
        
        # 默认建议
        if not recommendations:
            recommendations.append("内容分析完成，可进一步优化关键词策略")
        
        return recommendations[:5]  # 限制建议数量
    
    def get_analyzer_stats(self) -> Dict[str, Any]:
        """获取分析器统计信息"""
        return {
            'task_model_config': self.task_model_config,
            'ai_service_info': self.ai_service.get_model_info(),
            'supported_analysis': [
                'content_quality',
                'engagement_analysis',
                'theme_extraction',
                'sentiment_analysis',
                'readability_assessment',
                'feature_identification'
            ]
        } 