import openpyxl
from openpyxl.styles import Font, Alignment, Border, Side, PatternFill
from openpyxl.utils import get_column_letter

wb = openpyxl.Workbook()

# ========== Sheet 1: 知识点重复次数统计 ==========
ws1 = wb.active
ws1.title = "知识点统计"

# 样式
header_font = Font(name="微软雅黑", bold=True, size=11)
header_fill = PatternFill(start_color="4472C4", end_color="4472C4", fill_type="solid")
header_font_white = Font(name="微软雅黑", bold=True, size=11, color="FFFFFF")
word_fill = PatternFill(start_color="E2EFDA", end_color="E2EFDA", fill_type="solid")
pattern_fill = PatternFill(start_color="DAEEF3", end_color="DAEEF3", fill_type="solid")
warn_fill = PatternFill(start_color="FFF2CC", end_color="FFF2CC", fill_type="solid")
ok_fill = PatternFill(start_color="E2EFDA", end_color="E2EFDA", fill_type="solid")
thin_border = Border(
    left=Side(style="thin"), right=Side(style="thin"),
    top=Side(style="thin"), bottom=Side(style="thin")
)
center_align = Alignment(horizontal="center", vertical="center", wrap_text=True)
left_align = Alignment(horizontal="left", vertical="center", wrap_text=True)

# 标题行
ws1.merge_cells("A1:G1")
ws1["A1"] = "S3-U30-L3 霹雳飞船 — 知识点重复次数统计"
ws1["A1"].font = Font(name="微软雅黑", bold=True, size=14)
ws1["A1"].alignment = Alignment(horizontal="center", vertical="center")

# 表头
headers = ["类别", "知识点", "出现次数", "规范上限", "判定", "出现行（类型）"]
for col, h in enumerate(headers, 1):
    cell = ws1.cell(row=3, column=col, value=h)
    cell.font = header_font_white
    cell.fill = header_fill
    cell.alignment = center_align
    cell.border = thin_border

# 数据
data = [
    # 单词
    ["单词", "tooth", 3, "2–3次", "✅ 合规",
     "① 对话朗读「提出疑问」\n② 对话朗读「金牙？他牙齿是是金色吗？」\n③ 对话挖空「总结金牙故事」"],
    ["单词", "garage", 4, "2–3次", "⚠️ 超标1次",
     "① 对话朗读「Wander Garage」\n② 对话互动「这里是Wander Garage」\n③ 信息补词「买门牌」\n④ 阅读理解行"],
    ["单词", "gold", 4, "2–3次", "⚠️ 超标1次",
     "① 对话朗读「金牙？」\n② 图片拖拽「选金色」\n③ 对话挖空「喷金色漆」\n④ 阅读理解行"],
    ["单词", "large", 4, "2–3次", "⚠️ 超标1次",
     "① 对话朗读「总结金牙故事」\n② 图片单选「选大发动机」\n③ 对话选择「巨大车标」\n④ 阅读理解行"],
    ["单词", "similar", 3, "2–3次", "✅ 合规",
     "① 对话朗读「船很相似」\n② 挖空互动「尾翼像CEO的车」\n③ 阅读理解行"],
    ["单词", "teach", 3, "2–3次", "✅ 合规",
     "① 对话朗读「你可以教我」\n② 对话朗读「教我们什么」\n③ 对话挖空「Can somebody teach me?」"],
    # 句型
    ["句型", "How can I help you?", 2, "2–3次", "✅ 合规",
     "① 对话朗读「对顾客第一句」\n② 对话选择「Fox打招呼」"],
    ["句型", "How long have you been...", 2, "2–3次", "✅ 合规",
     "① 对话朗读「存钱多久」\n② 对话选读「认识彼此多久」"],
    ["句型", "Can I speak to...?", 2, "2–3次", "✅ 合规",
     "① 对话朗读「跟螺钉谈谈」\n② 对话组句「跟Shark聊聊」"],
]

for i, row in enumerate(data, 4):
    for col, val in enumerate(row, 1):
        cell = ws1.cell(row=i, column=col, value=val)
        cell.border = thin_border
        if col == 6:
            cell.alignment = left_align
        else:
            cell.alignment = center_align
        # 类别着色
        if row[0] == "单词":
            if col <= 5:
                cell.fill = word_fill
        else:
            if col <= 5:
                cell.fill = pattern_fill

# 汇总行
summary_row = len(data) + 4
ws1.merge_cells(f"A{summary_row}:G{summary_row}")
ws1.cell(row=summary_row, column=1, value="").border = thin_border

summary_row += 1
ws1.merge_cells(f"A{summary_row}:B{summary_row}")
ws1.cell(row=summary_row, column=1, value="汇总").font = Font(name="微软雅黑", bold=True, size=11)
ws1.cell(row=summary_row, column=1).alignment = center_align
ws1.cell(row=summary_row, column=1).fill = PatternFill(start_color="D9E2F3", end_color="D9E2F3", fill_type="solid")
for col in range(1, 8):
    ws1.cell(row=summary_row, column=col).border = thin_border

summary_data = [
    ["单词数", "6个", "6个", "✅"],
    ["句型数", "3个", "3个", "✅"],
    ["知识点互动次数", "2–3次", "2–4次", "⚠️ garage/gold/large 各4次"],
]
for j, sd in enumerate(summary_data):
    r = summary_row + 1 + j
    ws1.cell(row=r, column=1, value="").border = thin_border
    ws1.cell(row=r, column=2, value=sd[0]).font = Font(name="微软雅黑", bold=True)
    ws1.cell(row=r, column=2).alignment = center_align
    ws1.cell(row=r, column=2).border = thin_border
    ws1.cell(row=r, column=3, value="规范值").font = Font(name="微软雅黑", color="808080")
    ws1.cell(row=r, column=3).alignment = center_align
    ws1.cell(row=r, column=3).border = thin_border
    ws1.cell(row=r, column=4, value=sd[1]).alignment = center_align
    ws1.cell(row=r, column=4).border = thin_border
    ws1.cell(row=r, column=5, value="实际").font = Font(name="微软雅黑", color="808080")
    ws1.cell(row=r, column=5).alignment = center_align
    ws1.cell(row=r, column=5).border = thin_border
    ws1.cell(row=r, column=6, value=sd[2]).alignment = center_align
    ws1.cell(row=r, column=6).border = thin_border
    ws1.cell(row=r, column=7, value=sd[3]).alignment = center_align
    ws1.cell(row=r, column=7).border = thin_border

# 列宽
ws1.column_dimensions["A"].width = 8
ws1.column_dimensions["B"].width = 26
ws1.column_dimensions["C"].width = 12
ws1.column_dimensions["D"].width = 12
ws1.column_dimensions["E"].width = 14
ws1.column_dimensions["F"].width = 50

# ========== Sheet 2: 教研知识区对照 ==========
ws2 = wb.create_sheet("教研知识区对照")

ws2.merge_cells("A1:E1")
ws2["A1"] = "教研知识区知识点清单 vs 剧本表实际出现"
ws2["A1"].font = Font(name="微软雅黑", bold=True, size=13)
ws2["A1"].alignment = Alignment(horizontal="center", vertical="center")

headers2 = ["类别", "知识点", "教研区状态", "剧本表出现", "备注"]
for col, h in enumerate(headers2, 1):
    cell = ws2.cell(row=3, column=col, value=h)
    cell.font = header_font_white
    cell.fill = header_fill
    cell.alignment = center_align
    cell.border = thin_border

ref_data = [
    ["单词", "tooth", "✅ 已列入", "✅ 出现3次", ""],
    ["单词", "large", "✅ 已列入", "⚠️ 出现4次", "超标"],
    ["单词", "gold", "✅ 已列入", "⚠️ 出现4次", "超标"],
    ["单词", "garage", "✅ 已列入", "⚠️ 出现4次", "超标"],
    ["单词", "similar", "✅ 已列入", "✅ 出现3次", ""],
    ["单词", "teach", "✅ 已列入", "✅ 出现3次", ""],
    ["句型", "How can I help you?", "✅ 已列入", "✅ 出现2次", ""],
    ["句型", "Can I speak to...?", "✅ 已列入", "✅ 出现2次", ""],
    ["句型", "How long have you been...", "✅ 已列入", "✅ 出现2次", ""],
    ["句型", "When did the problem start?", "❌ 已删除", "❌ 未出现", "教研区已划删除线"],
    ["句型", "Would you mind if...?", "❌ 已删除", "❌ 未出现", "教研区已划删除线"],
    ["句型", "Something doesn't feel right.", "❌ 已删除", "❌ 未出现", "教研区已划删除线"],
]

for i, row in enumerate(ref_data, 4):
    for col, val in enumerate(row, 1):
        cell = ws2.cell(row=i, column=col, value=val)
        cell.border = thin_border
        cell.alignment = center_align if col <= 4 else left_align
        if row[0] == "单词":
            cell.fill = word_fill
        else:
            cell.fill = pattern_fill

ws2.column_dimensions["A"].width = 8
ws2.column_dimensions["B"].width = 32
ws2.column_dimensions["C"].width = 14
ws2.column_dimensions["D"].width = 16
ws2.column_dimensions["E"].width = 22

# 保存
output_path = "/root/.openclaw/workspace-xiaoyan/output/S3_U30_L3_知识点统计.xlsx"
wb.save(output_path)
print(f"Saved: {output_path}")
