# -*- coding: utf-8 -*-
import re

def parse_file(filepath):
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()
    
    result = {}
    # Split by "需要等级" to find each skill block - the skill name is before it
    parts = re.split(r'(需要等级\s+\d+)', content)
    
    for i in range(1, len(parts), 2):
        if i+1 >= len(parts):
            break
        level_line = parts[i]  # "需要等级 12"
        level_match = re.search(r'需要等级\s+(\d+)', level_line)
        if not level_match:
            continue
        level = int(level_match.group(1))
        if level % 2 != 0:
            continue
        
        after = parts[i+1]
        learn_match = re.search(r'学习：\s*(.+?)(?:\n|$)', after)
        if not learn_match or '默认开启' in learn_match.group(1):
            continue
        
        # Get skill name from the part before "需要等级"
        before = parts[i-1]
        lines = before.strip().split('\n')
        skill_line = None
        for line in reversed(lines):
            line = line.strip()
            if not line or 'javascript' in line or line.startswith('['):
                continue
            if re.match(r'^[\u4e00-\u9fff\s：]+(等级\s+\d+)?\s*$', line) and len(line) < 50:
                skill_line = line
                break
        if not skill_line:
            continue
        
        rank_match = re.match(r'^(.+?)\s+等级\s+(\d+)\s*$', skill_line)
        if rank_match:
            skill_base = rank_match.group(1).strip()
            rank = int(rank_match.group(2))
            display = skill_base if rank == 1 else f"{skill_base} {rank}级"
        else:
            display = skill_line
        
        if any(x in display for x in ['魔兽世界', '职业', '需要', '·']):
            continue
            
        if level not in result:
            result[level] = []
        if display not in result[level]:
            result[level].append(display)
    
    return dict(sorted(result.items()))

def format_lua(data, name):
    lines = [f"{name} = {{"]
    for level, skills in sorted(data.items()):
        skills_str = ", ".join(f'"{s}"' for s in sorted(skills))
        lines.append(f"    [{level}] = {{{skills_str}}},")
    lines.append("},")
    return "\n".join(lines)

priest_file = r'C:\Users\rucky\.cursor\projects\e-Game-trutle-wow-Interface-AddOns-Nanami-UI\agent-tools\8aaa3634-8b06-4c6a-838c-18f248f9b747.txt'
shaman_file = r'C:\Users\rucky\.cursor\projects\e-Game-trutle-wow-Interface-AddOns-Nanami-UI\agent-tools\e5e3b3f9-edfa-4a99-a95e-c9f7ed954371.txt'

priest_data = parse_file(priest_file)
shaman_data = parse_file(shaman_file)

output = format_lua(priest_data, "PRIEST") + "\n\n" + format_lua(shaman_data, "SHAMAN")
outpath = r'C:\Users\rucky\.cursor\projects\e-Game-trutle-wow-Interface-AddOns-Nanami-UI\agent-tools\class_trainer_skills.lua'
with open(outpath, 'w', encoding='utf-8') as f:
    f.write(output)
print("Done. Output written to class_trainer_skills.lua")
