#!/usr/bin/env python3
"""
Gift-Advisor 优化版评测报告收集系统
专家通过此系统提交评测报告
"""
import json
import os
import sys
from datetime import datetime
from typing import Dict, List, Optional

class EvaluationCollector:
    """评测报告收集器"""
    
    def __init__(self, data_dir: str = "evaluation_data"):
        self.data_dir = data_dir
        self.reports_file = os.path.join(data_dir, "evaluation_reports.json")
        self.summary_file = os.path.join(data_dir, "evaluation_summary.json")
        
        # 创建数据目录
        os.makedirs(data_dir, exist_ok=True)
        
        # 初始化数据文件
        if not os.path.exists(self.reports_file):
            self._init_reports_file()
    
    def _init_reports_file(self):
        """初始化评测报告文件"""
        data = {
            "project": "Gift-Advisor",
            "version": "2.0.0",
            "optimization_target": "8.0 → 9.0+",
            "evaluation_period": "2026-03-31 to 2026-04-02",
            "total_experts": 7,
            "reports": []
        }
        self._save_json(self.reports_file, data)
    
    def submit_report(self, report_data: Dict) -> Dict:
        """
        提交评测报告
        
        Args:
            report_data: 评测报告数据
            
        Returns:
            提交结果
        """
        try:
            # 验证必填字段
            required_fields = ["expert_id", "expert_name", "final_score", "optimization_score"]
            for field in required_fields:
                if field not in report_data:
                    return {
                        "success": False,
                        "message": f"缺少必填字段: {field}",
                        "required_fields": required_fields
                    }
            
            # 添加提交时间
            report_data["submission_time"] = datetime.now().isoformat()
            report_data["report_id"] = f"report_{len(self.get_all_reports()) + 1}"
            
            # 加载现有报告
            data = self._load_json(self.reports_file)
            
            # 检查是否已提交
            existing = [r for r in data["reports"] if r.get("expert_id") == report_data["expert_id"]]
            if existing:
                return {
                    "success": False,
                    "message": "该专家已提交过评测报告",
                    "existing_report": existing[0]
                }
            
            # 添加新报告
            data["reports"].append(report_data)
            
            # 保存
            self._save_json(self.reports_file, data)
            
            # 更新汇总
            self._update_summary()
            
            return {
                "success": True,
                "message": "评测报告提交成功",
                "report_id": report_data["report_id"],
                "submission_time": report_data["submission_time"]
            }
            
        except Exception as e:
            return {
                "success": False,
                "message": f"提交失败: {str(e)}"
            }
    
    def get_all_reports(self) -> List[Dict]:
        """获取所有评测报告"""
        data = self._load_json(self.reports_file)
        return data.get("reports", [])
    
    def get_report_by_id(self, report_id: str) -> Optional[Dict]:
        """根据ID获取报告"""
        reports = self.get_all_reports()
        for report in reports:
            if report.get("report_id") == report_id:
                return report
        return None
    
    def get_summary(self) -> Dict:
        """获取评测汇总"""
        if os.path.exists(self.summary_file):
            return self._load_json(self.summary_file)
        else:
            return self._update_summary()
    
    def _update_summary(self) -> Dict:
        """更新评测汇总"""
        reports = self.get_all_reports()
        
        if not reports:
            summary = {
                "total_reports": 0,
                "average_final_score": 0,
                "average_optimization_score": 0,
                "score_distribution": {},
                "status": "等待评测报告"
            }
        else:
            # 计算平均分
            final_scores = [r.get("final_score", 0) for r in reports]
            optimization_scores = [r.get("optimization_score", 0) for r in reports]
            
            # 分数分布
            score_dist = {}
            for score in final_scores:
                key = f"{score:.1f}"
                score_dist[key] = score_dist.get(key, 0) + 1
            
            summary = {
                "total_reports": len(reports),
                "average_final_score": sum(final_scores) / len(final_scores),
                "average_optimization_score": sum(optimization_scores) / len(optimization_scores),
                "score_distribution": score_dist,
                "status": f"已收集 {len(reports)}/7 份报告",
                "reports_by_score": sorted(reports, key=lambda x: x.get("final_score", 0), reverse=True)
            }
        
        self._save_json(self.summary_file, summary)
        return summary
    
    def generate_evaluation_report(self) -> Dict:
        """生成完整评测报告"""
        summary = self.get_summary()
        reports = self.get_all_reports()
        
        # 分析优化效果
        optimization_analysis = {
            "taboo_system": self._analyze_feature(reports, "taboo_system"),
            "blessing_system": self._analyze_feature(reports, "blessing_system"),
            "dialog_system": self._analyze_feature(reports, "dialog_system"),
            "overall_experience": self._analyze_feature(reports, "overall_experience")
        }
        
        # 收集专家建议
        suggestions = []
        for report in reports:
            if "suggestions" in report:
                suggestions.extend(report["suggestions"])
        
        # 生成最终报告
        final_report = {
            "project": "Gift-Advisor",
            "version": "2.0.0",
            "evaluation_date": datetime.now().isoformat(),
            "summary": summary,
            "optimization_analysis": optimization_analysis,
            "expert_suggestions": suggestions[:10],  # 最多10条建议
            "verdict": self._generate_verdict(summary),
            "recommendations": self._generate_recommendations(optimization_analysis)
        }
        
        # 保存报告
        report_file = os.path.join(self.data_dir, "final_evaluation_report.json")
        self._save_json(report_file, final_report)
        
        return final_report
    
    def _analyze_feature(self, reports: List[Dict], feature_key: str) -> Dict:
        """分析特定功能"""
        scores = []
        comments = []
        
        for report in reports:
            if "feature_scores" in report and feature_key in report["feature_scores"]:
                scores.append(report["feature_scores"][feature_key])
            
            if "comments" in report and feature_key in report["comments"]:
                comments.append(report["comments"][feature_key])
        
        if scores:
            return {
                "average_score": sum(scores) / len(scores),
                "max_score": max(scores),
                "min_score": min(scores),
                "sample_count": len(scores),
                "comments": comments[:5]  # 最多5条评论
            }
        else:
            return {
                "average_score": 0,
                "sample_count": 0,
                "comments": []
            }
    
    def _generate_verdict(self, summary: Dict) -> str:
        """生成评测结论"""
        avg_score = summary.get("average_final_score", 0)
        
        if summary.get("total_reports", 0) < 3:
            return "评测样本不足，需要更多专家参与"
        
        if avg_score >= 9.0:
            return f"✅ 优化成功！平均评分 {avg_score:.1f}/10，达到9.0+目标"
        elif avg_score >= 8.5:
            return f"⚠️ 优化有效但未达标！平均评分 {avg_score:.1f}/10，接近目标"
        elif avg_score >= 8.0:
            return f"❌ 优化效果有限！平均评分 {avg_score:.1f}/10，与原版持平"
        else:
            return f"❌ 优化失败！平均评分 {avg_score:.1f}/10，低于原版"
    
    def _generate_recommendations(self, analysis: Dict) -> List[str]:
        """生成改进建议"""
        recommendations = []
        
        # 禁忌系统建议
        taboo_score = analysis["taboo_system"]["average_score"]
        if taboo_score < 8.0:
            recommendations.append("禁忌系统需要加强，建议扩充禁忌库并优化解释")
        
        # 祝福语系统建议
        blessing_score = analysis["blessing_system"]["average_score"]
        if blessing_score < 8.0:
            recommendations.append("祝福语系统需要更多个性化选项，建议增加方言和风格")
        
        # 对话系统建议
        dialog_score = analysis["dialog_system"]["average_score"]
        if dialog_score < 8.0:
            recommendations.append("对话系统需要优化交互流畅度，建议改进意图识别")
        
        return recommendations
    
    def _load_json(self, filepath: str) -> Dict:
        """加载JSON文件"""
        with open(filepath, 'r', encoding='utf-8') as f:
            return json.load(f)
    
    def _save_json(self, filepath: str, data: Dict):
        """保存JSON文件"""
        with open(filepath, 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=2)


# Web界面模拟
class WebInterface:
    """Web界面模拟（供专家使用）"""
    
    def __init__(self, collector: EvaluationCollector):
        self.collector = collector
    
    def show_submission_form(self):
        """显示提交表单"""
        form_template = """
        ===========================================
        Gift-Advisor 优化版评测报告提交系统
        ===========================================
        
        请填写评测报告：
        
        1. 专家信息
           - 专家ID: ______
           - 姓名/昵称: ______
           - 专业领域: ______
        
        2. 核心评分（10分制）
           - 最终评分: ___/10
           - 优化提升分: ___分（原8.0分）
        
        3. 功能专项评分（可选）
           - 禁忌过滤系统: ___/10
           - 祝福语系统: ___/10
           - 对话系统: ___/10
           - 综合体验: ___/10
        
        4. 评价与建议
           - 优点: ______
           - 改进建议: ______
           - 重大发现: ______
        
        5. 推荐意见
           - [ ] 推荐上架 ClawHub
           - [ ] 需要进一步优化
           - [ ] 不推荐当前版本
        
        ===========================================
        """
        print(form_template)
    
    def submit_from_input(self):
        """从输入提交报告"""
        print("请填写评测报告：")
        
        report = {
            "expert_id": input("专家ID: "),
            "expert_name": input("姓名/昵称: "),
            "expert_field": input("专业领域: "),
            "final_score": float(input("最终评分(0-10): ")),
            "optimization_score": float(input("优化提升分(相对于原版): ")),
            "feature_scores": {},
            "comments": {},
            "suggestions": []
        }
        
        # 功能专项评分
        print("\n功能专项评分（可选，直接回车跳过）:")
        features = ["taboo_system", "blessing_system", "dialog_system", "overall_experience"]
        feature_names = ["禁忌过滤系统", "祝福语系统", "对话系统", "综合体验"]
        
        for i, feature in enumerate(features):
            score = input(f"{feature_names[i]}(0-10): ")
            if score:
                report["feature_scores"][feature] = float(score)
                comment = input(f"  {feature_names[i]}评价: ")
                if comment:
                    report["comments"][feature] = comment
        
        # 建议
        print("\n改进建议（每行一条，输入空行结束）:")
        while True:
            suggestion = input("建议: ")
            if not suggestion:
                break
            report["suggestions"].append(suggestion)
        
        # 推荐意见
        print("\n推荐意见:")
        print("1. 推荐上架 ClashHub")
        print("2. 需要进一步优化")
        print("3. 不推荐当前版本")
        choice = input("选择(1-3): ")
        if choice == "1":
            report["recommendation"] = "recommend"
        elif choice == "2":
            report["recommendation"] = "need_optimization"
        else:
            report["recommendation"] = "not_recommend"
        
        # 提交
        result = self.collector.submit_report(report)
        print(f"\n提交结果: {result['message']}")
        
        if result["success"]:
            print(f"报告ID: {result['report_id']}")
            print(f"提交时间: {result['submission_time']}")
        
        return result


# 命令行接口
def main