#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
最终测试航班评价分析的区分度 - 验证所有修复
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from smart_customer_service import SmartCustomerService

def final_test_airline():
    """最终测试航班评价分析的区分度"""
    
    print("=== 最终测试航班评价分析的区分度 ===")
    print("目标：验证所有修复是否解决了过度敏感和情感分析错误问题")
    print()
    
    # 创建客服实例并训练模型
    customer_service = SmartCustomerService("../data/Tweets.csv")
    customer_service.train_churn_model()
    
    # 定义不同负面程度的航班评价示例
    test_cases = [
        {
            "name": "严重负面 - 环境恶劣+服务差",
            "text": "旁边人太吵了，还脱鞋，空姐不管，环境脏乱差，服务态度极其恶劣",
            "expected_sentiment": "negative",
            "expected_risk": "high"
        },
        {
            "name": "中等负面 - 服务问题", 
            "text": "空姐服务态度一般，有点冷漠，但整体还可以",
            "expected_sentiment": "negative",
            "expected_risk": "medium"
        },
        {
            "name": "轻微负面 - 小问题",
            "text": "飞机上有点吵，其他都挺好的",
            "expected_sentiment": "negative",
            "expected_risk": "low"
        },
        {
            "name": "正面评价",
            "text": "空姐服务很热情，环境干净舒适，飞行很平稳",
            "expected_sentiment": "positive",
            "expected_risk": "low"
        },
        {
            "name": "中性评价",
            "text": "航班准时到达，服务还可以",
            "expected_sentiment": "neutral",
            "expected_risk": "low"
        },
        {
            "name": "严重食品问题",
            "text": "飞机餐都馊了，变质发霉，太恶心了",
            "expected_sentiment": "negative",
            "expected_risk": "high"
        },
        {
            "name": "延误问题",
            "text": "航班延误了3个小时，等待时间太长，很不满意",
            "expected_sentiment": "negative",
            "expected_risk": "medium"
        },
        {
            "name": "行李问题",
            "text": "行李托运损坏了，机场工作人员处理不及时",
            "expected_sentiment": "negative",
            "expected_risk": "medium"
        }
    ]
    
    # 模拟用户数据（中等风险用户）
    user_data = {
        "name": "测试用户",
        "age": 35,
        "gender": "female", 
        "location": "北京",
        "account_age": 365,
        "total_orders": 15,
        "avg_order_value": 150.0,
        "complaint_count": 2,
        "last_complaint_days": 30,
        "satisfaction_score": 3.5,
        "response_time_avg": 120,
        "response_time_std": 45,
        "conversation_count": 25,
        "negative_feedback_count": 3,
        "positive_feedback_count": 8,
        "neutral_feedback_count": 14,
        "churn_probability": 0.3
    }
    
    results = []
    
    for i, case in enumerate(test_cases, 1):
        print(f"\n{i}. {case['name']}")
        print(f"   评价内容: {case['text']}")
        
        # 分析对话内容
        analysis = customer_service.analyze_conversation_content(case['text'], "")
        
        # 进行流失风险预测
        churn_prediction = customer_service.predict_churn_risk_with_conversation(user_data, analysis)
        
        # 提取关键指标
        sentiment = analysis.get('sentiment', 'neutral')
        intensity = analysis.get('complaint_intensity', 0)
        keywords_count = len(analysis.get('keywords', []))
        churn_prob = churn_prediction.get('churn_probability', 0)
        churn_level = churn_prediction.get('churn_level', 'unknown')
        
        # 判断是否合理
        sentiment_correct = sentiment == case['expected_sentiment']
        risk_correct = churn_level == case['expected_risk']
        
        # 检查流失概率是否合理
        prob_reasonable = True
        if case['expected_risk'] == 'high' and churn_prob < 0.6:
            prob_reasonable = False
        elif case['expected_risk'] == 'medium' and (churn_prob < 0.4 or churn_prob > 0.8):
            prob_reasonable = False
        elif case['expected_risk'] == 'low' and churn_prob > 0.4:
            prob_reasonable = False
        
        print(f"   情感分析: {sentiment} ({'✅' if sentiment_correct else '❌'})")
        print(f"   投诉强度: {intensity:.1%}")
        print(f"   关键词数: {keywords_count}")
        print(f"   流失概率: {churn_prob:.2%} ({'✅' if prob_reasonable else '❌'})")
        print(f"   流失等级: {churn_level} ({'✅' if risk_correct else '❌'})")
        
        if keywords_count > 0:
            keywords = analysis.get('keywords', [])[:3]
            print(f"   关键词: {', '.join([k['word'] for k in keywords])}")
        
        results.append({
            "case": case,
            "sentiment_correct": sentiment_correct,
            "risk_correct": risk_correct,
            "prob_reasonable": prob_reasonable
        })
    
    # 总体评估
    print("\n" + "="*60)
    sentiment_correct_count = sum(1 for r in results if r['sentiment_correct'])
    risk_correct_count = sum(1 for r in results if r['risk_correct'])
    prob_reasonable_count = sum(1 for r in results if r['prob_reasonable'])
    total_count = len(results)
    
    print(f"情感分析正确率: {sentiment_correct_count}/{total_count}")
    print(f"风险等级正确率: {risk_correct_count}/{total_count}")
    print(f"流失概率合理率: {prob_reasonable_count}/{total_count}")
    
    # 显示流失概率分布
    probs = [r['case']['text'] for r in results]
    churn_probs = [customer_service.predict_churn_risk_with_conversation(user_data, 
                    customer_service.analyze_conversation_content(text, "")).get('churn_probability', 0) 
                   for text in probs]
    
    print(f"\n流失概率分布: {min(churn_probs):.2%} - {max(churn_probs):.2%}")
    
    if sentiment_correct_count == total_count and risk_correct_count == total_count and prob_reasonable_count == total_count:
        print("\n🎉 所有问题都已彻底修复！系统现在可以正确分析各种评价。")
        
        # 显示修复效果对比
        print("\n=== 修复效果对比 ===")
        print("修复前: 几乎所有负面评论都被识别为99%流失率")
        print("修复后: 流失概率有合理的梯度分布")
        print("修复前: 情感分析错误（正面评价被识别为负面）")
        print("修复后: 情感分析正确")
        print("修复前: 风险等级不合理")
        print("修复后: 风险等级有合理区分")
        
    else:
        print("\n⚠️ 部分问题仍需进一步优化。")
        
        # 显示有问题的案例
        problematic_cases = [r for r in results if not (r['sentiment_correct'] and r['risk_correct'] and r['prob_reasonable'])]
        if problematic_cases:
            print("需要进一步优化的案例:")
            for r in problematic_cases:
                issues = []
                if not r['sentiment_correct']:
                    issues.append("情感分析错误")
                if not r['risk_correct']:
                    issues.append("风险等级错误")
                if not r['prob_reasonable']:
                    issues.append("流失概率不合理")
                print(f"- {r['case']['name']}: {', '.join(issues)}")

if __name__ == "__main__":
    final_test_airline()