import pandas as pd
import numpy as np
from smart_customer_service import SmartCustomerService

def diagnose_churn_system():
    """诊断流失预测系统的问题"""
    print("=== 智能客服系统诊断 ===")
    
    # 初始化系统
    service = SmartCustomerService('../data/Tweets.csv')
    
    # 1. 检查模型训练
    print("\n1. 检查模型训练...")
    if service.train_churn_model():
        print("✅ 模型训练成功")
        
        # 检查模型类型
        print(f"模型类型: {type(service.churn_model)}")
        
        # 检查特征维度
        if hasattr(service.churn_model, 'feature_importances_'):
            print(f"特征重要性: {service.churn_model.feature_importances_}")
    else:
        print("❌ 模型训练失败")
        return
    
    # 2. 测试预测逻辑
    print("\n2. 测试预测逻辑...")
    
    # 测试不同用户
    test_users = ['cairdin', 'jnardino', 'yvonnalynn']
    
    for user_id in test_users:
        print(f'\n--- 测试用户: {user_id} ---')
        
        # 获取用户数据
        user_data = service.get_user_data(user_id)
        
        if 'error' not in user_data:
            print(f"用户数据特征:")
            for key, value in user_data.items():
                if isinstance(value, (int, float)):
                    print(f"  {key}: {value}")
            
            # 预测流失风险
            churn_prediction = service.predict_churn_risk(user_data)
            
            if 'error' not in churn_prediction:
                print(f"预测结果:")
                print(f"  流失概率: {churn_prediction.get('churn_probability', 0):.4f}")
                print(f"  流失等级: {churn_prediction.get('churn_level', 'unknown')}")
                print(f"  风险因素: {churn_prediction.get('risk_factors', [])}")
                
                # 检查概率是否合理
                prob = churn_prediction.get('churn_probability', 0)
                if prob == 0.0:
                    print("⚠️ 警告: 流失概率为0.00%，可能存在模型问题")
            else:
                print(f"❌ 预测失败: {churn_prediction['error']}")
        else:
            print(f"❌ 获取用户数据失败: {user_data['error']}")
    
    # 3. 检查流失分析功能
    print("\n3. 检查流失分析功能...")
    
    # 检查流失分析模块是否存在
    if hasattr(service, 'show_churn_analysis'):
        print("✅ 流失分析模块存在")
    else:
        print("❌ 流失分析模块不存在")
    
    # 4. 检查DeepSeek集成
    print("\n4. 检查DeepSeek集成...")
    
    # 测试智能客服功能
    test_query = "我对航班延误很不满意"
    test_user = "cairdin"
    
    print(f"测试用户: {test_user}")
    print(f"测试查询: {test_query}")
    
    result = service.process_customer_query(test_user, test_query)
    
    if 'response' in result:
        print("✅ 智能客服响应成功生成")
        
        # 检查响应内容
        response = result['response']
        if "流失风险" in response or "流失概率" in response:
            print("✅ 响应中包含流失相关信息")
        else:
            print("⚠️ 响应中缺少流失相关信息")
            
        # 检查DeepSeek内容
        if len(response) > 500:
            print("✅ 响应内容丰富，可能包含DeepSeek生成内容")
        else:
            print("⚠️ 响应内容可能过于简单")
    else:
        print("❌ 智能客服响应失败")

if __name__ == "__main__":
    diagnose_churn_system()