import re

with open('index_main.html','r',encoding='utf-8') as f:
    html = f.read()

# 5 条新 PROGRAMS 记录
new_programs = [
    {
        "id": "notre-mscse",
        "s": "notre",
        "n": "计算机科学与工程硕士",
        "en": "M.S. in Computer Science and Engineering (MSCSE)",
        "dept": "工程学院 CSE 系",
        "college": "工程学院",
        "disc": "cs",
        "lvl": "m",
        "deg": "MS",
        "dur": "11 个月（两学期 + 夏季）",
        "stem": 1,
        "rank": "USNews 2026 全美第 18",
        "test_cls": "req",
        "ddl": "2027 秋：2026-12-15；2027 春：2026-10-15",
        "lang": "TOEFL 80（口语23）/ IELTS 7.0 / Duolingo 120",
        "tui": "自费专业型硕士，通常无奖学金；TA 职位竞争",
        "url": "details/notre-computer-science-and-engineering-mscse---masters-professional.html"
    },
    {
        "id": "notre-msee",
        "s": "notre",
        "n": "电气工程硕士",
        "en": "M.S. in Electrical Engineering (MSEE)",
        "dept": "工程学院 EE 系",
        "college": "工程学院",
        "disc": "ee",
        "lvl": "m",
        "deg": "MS",
        "dur": "1.5–2 年",
        "stem": 1,
        "rank": "USNews 2026 全美第 18",
        "test_cls": "rec",
        "ddl": "2027 秋：2026-12-15（仅秋季）",
        "lang": "TOEFL 80（口语23）/ IELTS 7.0 / Duolingo 120",
        "tui": "部分学生可获 RA/TA 资助",
        "url": "details/notre-electrical-engineering-msee---masters-traditional.html"
    },
    {
        "id": "notre-msme",
        "s": "notre",
        "n": "机械工程硕士",
        "en": "M.S. in Mechanical Engineering (MSME)",
        "dept": "工程学院 航空与机械工程系",
        "college": "工程学院",
        "disc": "mech",
        "lvl": "m",
        "deg": "MS",
        "dur": "1.5–2 年",
        "stem": 1,
        "rank": "USNews 2026 全美第 18",
        "test_cls": "opt",
        "ddl": "2027 秋：2026-12-31；2027 春：2026-10-15",
        "lang": "TOEFL 80（口语23）/ IELTS 7.0 / Duolingo 120",
        "tui": "多数 PhD 获全额资助，MS 名额有限",
        "url": "details/notre-mechanical-engineering-msme---masters-traditional.html"
    },
    {
        "id": "unc-stats-ds",
        "s": "unc",
        "n": "统计、分析与数据科学硕士",
        "en": "M.S. in Statistics, Analytics, and Data Science",
        "dept": "统计与运筹系（STOR）",
        "college": "文理学院",
        "disc": "stat",
        "lvl": "m",
        "deg": "MS",
        "dur": "1.5–2 年（3–4 学期）",
        "stem": 1,
        "rank": "USNews 2026 全美第 28",
        "test_cls": "notreq",
        "ddl": "秋季入学，滚动审核",
        "lang": "TOEFL 90 / IELTS 7.0 / Duolingo 110",
        "tui": "MS 资助名额有限",
        "url": "details/unc-statistics-analytics-and-data-science-ms.html"
    },
    {
        "id": "unc-bhmi",
        "s": "unc",
        "n": "生物医学与健康信息学专业硕士",
        "en": "MPS in Biomedical and Health Informatics (On-Campus)",
        "dept": "Carolina Health Informatics Program（CHIP）",
        "college": "跨学科联合培养",
        "disc": "bio",
        "lvl": "m",
        "deg": "MPS",
        "dur": "1–3 年（全日制/非全日制）",
        "stem": 1,
        "rank": "USNews 2026 全美第 28",
        "test_cls": "notreq",
        "ddl": "2027 秋：2027-04-13（在校版）",
        "lang": "TOEFL 90 / IELTS 7.0 / Duolingo 110",
        "tui": "专业型硕士，资助名额有限",
        "url": "details/unc-biomedical-and-health-informatics-ms.html"
    },
]

# 生成 PROGRAMS 记录字符串
def make_prog_str(p):
    return (f'{{id:"{p["id"]}",s:"{p["s"]}",n:"{p["n"]}",en:"{p["en"]}",'
            f'dept:"{p["dept"]}",college:"{p["college"]}",disc:"{p["disc"]}",'
            f'lvl:"{p["lvl"]}",deg:"{p["deg"]}",dur:"{p["dur"]}",stem:{p["stem"]},'
            f'rank:"{p["rank"]}",\n'
            f' test:{{cls:"{p["test_cls"]}"}},ddl:"{p["ddl"]}",lang:"{p["lang"]}",'
            f'tui:"{p["tui"]}",\n'
            f' url:"{p["url"]}"}}')

# 找 PROGRAMS 数组结束位置（括号深度匹配）
prog_start = html.find('const PROGRAMS=[')
if prog_start < 0:
    print("ERROR: 找不到 const PROGRAMS=[")
    exit(1)

# 从 [ 开始数括号深度
depth = 0
i = html.index('[', prog_start)
end_pos = -1
while i < len(html):
    c = html[i]
    if c == '[':
        depth += 1
    elif c == ']':
        depth -= 1
        if depth == 0:
            end_pos = i
            break
    i += 1

if end_pos < 0:
    print("ERROR: 找不到 PROGRAMS 数组结束位置")
    exit(1)

print(f"PROGRAMS 数组结束位置: {end_pos}")
print(f"结束前片段: ...{html[end_pos-30:end_pos]}")

# 在结束前插入新记录
# 先看最后一条记录的格式
before_end = html[prog_start:end_pos]
print(f"\nPROGRAMS 数组长度: {len(before_end)} 字符")

# 构建新记录字符串
new_records = ",\n" + ",\n".join(make_prog_str(p) for p in new_programs)

# 插入到 ] 之前
new_html = html[:end_pos] + new_records + "\n" + html[end_pos:]

# === 追加 VD 集合 ===
vd_match = re.search(r'const VERIFIED_DETAILS=new Set\(\[', new_html)
if not vd_match:
    print("ERROR: 找不到 VERIFIED_DETAILS")
    exit(1)

vd_start = vd_match.end() - 1  # 指向 [
vd_depth = 0
j = vd_start
vd_end = -1
while j < len(new_html):
    c = new_html[j]
    if c == '[':
        vd_depth += 1
    elif c == ']':
        vd_depth -= 1
        if vd_depth == 0:
            vd_end = j
            break
    j += 1

print(f"VD 集合结束位置: {vd_end}")

# 追加 5 条到 VD
vd_new_entries = ",".join(f'"{p["url"]}"' for p in new_programs)
new_html = new_html[:vd_end] + "," + vd_new_entries + new_html[vd_end:]

# 写回文件
with open('index_main.html','w',encoding='utf-8') as f:
    f.write(new_html)

print(f"\n✅ 已追加 {len(new_programs)} 条 PROGRAMS 记录")
print(f"✅ 已追加 {len(new_programs)} 条 VD 登记")
print(f"文件大小: {len(new_html)} 字符")
