import pandas as pd

# ==============================
# 1. 基础配置
# ==============================

file_path = '/root/.openclaw/media/inbound/è_è_²id_2827_å_¼å_ºæ_é_20260316---befdf3d9-0682-46df-aea5-74839af2a1cd.xlsx'
student_name = '角色ID2827'

# ==============================
# 2. 读取Excel数据
# ==============================

kp_stats = pd.read_excel(file_path, sheet_name='统计-知识点通过情况')
component_stats = pd.read_excel(file_path, sheet_name='统计-互动组件通过情况')

# ==============================
# 3. 数据清洗（防止空值）
# ==============================

kp_stats = kp_stats.fillna(0)

# ==============================
# 4. 计算知识点加权得分
# ==============================

kp_stats['weighted_score'] = (
    kp_stats['Perfect数量'] * 100 +
    kp_stats['Good数量'] * 80 +
    kp_stats['Pass数量'] * 60
) / kp_stats['总数量']

# ==============================
# 5. 计算正确率
# ==============================

kp_stats['correct_rate'] = (
    kp_stats['Perfect数量'] +
    kp_stats['Good数量'] +
    kp_stats['Pass数量']
) / kp_stats['总数量']

# ==============================
# 6. 计算能力模块得分
# ==============================

vocab_score = kp_stats[kp_stats['知识点类型'] == 'vocab']['weighted_score'].mean()
sentence_score = kp_stats[kp_stats['知识点类型'] == 'sentence']['weighted_score'].mean()

# ==============================
# 7. 综合得分
# ==============================

overall_score = kp_stats['weighted_score'].mean()
overall_correct_rate = kp_stats['correct_rate'].mean()

# ==============================
# 8. 等级判断
# ==============================

def get_level(score):
    if score >= 90:
        return '优秀'
    elif score >= 80:
        return '良好'
    elif score >= 70:
        return '合格'
    else:
        return '需要提升'

level = get_level(overall_score)

# ==============================
# 9. 找出薄弱知识点
# ==============================

weak_kp = kp_stats.sort_values('weighted_score').head(5)

# ==============================
# 10. 生成报告数据
# ==============================

report_data = {
    '学生姓名': student_name,
    '综合得分': round(overall_score, 1),
    '词汇能力得分': round(vocab_score, 1),
    '句子能力得分': round(sentence_score, 1),
    '总体正确率': f"{round(overall_correct_rate*100,1)}%",
    '学习水平等级': level
}

report_df = pd.DataFrame([report_data])

# ==============================
# 11. 导出Excel报告
# ==============================

output_file = '学习分析报告_自动生成版.xlsx'

with pd.ExcelWriter(output_file) as writer:

    # 总结报告
    report_df.to_excel(
        writer,
        sheet_name='学习报告',
        index=False
    )

    # 知识点详情
    kp_stats.to_excel(
        writer,
        sheet_name='知识点详情',
        index=False
    )

    # 薄弱知识点
    weak_kp.to_excel(
        writer,
        sheet_name='薄弱知识点TOP5',
        index=False
    )

print(f"✅ 学习报告生成完成：{output_file}")