# -*- coding: utf-8 -*-
import io, sys, os, re
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")

p = "index_main.html"
idx = open(p, encoding="utf-8").read()

# 待追加 5 条 PROGRAMS
new_progs = [
    # id, s, n, en, dept, college, disc, lvl, deg, dur, stem, rank, test_cls, ddl, lang, tui, url
    ('{id:"unc-chemistry-ms",s:"unc",n:"化学硕士",en:"Master of Science in Chemistry",dept:"Department of Chemistry",college:"College of Arts and Sciences",disc:"sci",lvl:"m",deg:"MS",dur:"2年",stem:1,test:{cls:""},ddl:"",lang:"",tui:"",url:"details/unc-chemistry-ms.html"}',),
    ('{id:"unc-hpm-msph",s:"unc",n:"卫生政策与管理研究型公卫硕士",en:"Master of Science in Public Health in Health Policy and Management",dept:"Department of Health Policy and Management",college:"Gillings School of Global Public Health",disc:"med",lvl:"m",deg:"MSPH",dur:"2年",stem:0,test:{cls:""},ddl:"",lang:"",tui:"",url:"details/unc-hpm-msph.html"}',),
    ('{id:"unc-epi-ms",s:"unc",n:"流行病学硕士",en:"Master of Science in Epidemiology",dept:"Department of Epidemiology",college:"Gillings School of Global Public Health",disc:"med",lvl:"m",deg:"MS",dur:"2年",stem:0,test:{cls:""},ddl:"",lang:"",tui:"",url:"details/unc-epi-ms.html"}',),
    ('{id:"unc-nutrition-ms",s:"unc",n:"营养科学硕士",en:"Master of Science in Nutrition",dept:"Department of Nutrition",college:"Gillings School of Global Public Health",disc:"med",lvl:"m",deg:"MS",dur:"2年",stem:0,test:{cls:""},ddl:"",lang:"",tui:"",url:"details/unc-nutrition-ms.html"}',),
    ('{id:"unc-polisci-tam-ma",s:"unc",n:"政治学跨大西洋双硕士（TAM）",en:"Master of Arts in Political Science, TransAtlantic Masters Program",dept:"Department of Political Science",college:"College of Arts and Sciences",disc:"soc",lvl:"m",deg:"MA",dur:"2年（1 UNC + 1 欧洲）",stem:0,test:{cls:"req"},ddl:"12/1优先",lang:"",tui:"",url:"details/unc-polisci-tam-ma.html"}',),
]

# 待追加 5 条 CATD
new_catd = [
    '["unc","Department of Chemistry","Master of Science in Chemistry","化学硕士（MS）","MS","https://catalog.unc.edu/graduate/schools-departments/chemistry/","sci","details/unc-chemistry-ms.html"]',
    '["unc","Department of Health Policy and Management, Gillings School of Global Public Health","Master of Science in Public Health in Health Policy and Management","卫生政策与管理研究型公卫硕士（MSPH）","MSPH","https://catalog.unc.edu/graduate/schools-departments/health-policy-management/","med","details/unc-hpm-msph.html"]',
    '["unc","Department of Epidemiology, Gillings School of Global Public Health","Master of Science in Epidemiology","流行病学硕士（MS）","MS","https://catalog.unc.edu/graduate/schools-departments/epidemiology/","med","details/unc-epi-ms.html"]',
    '["unc","Department of Nutrition, Gillings School of Global Public Health","Master of Science in Nutrition","营养科学硕士（MS）","MS","https://catalog.unc.edu/graduate/schools-departments/nutrition/","med","details/unc-nutrition-ms.html"]',
    '["unc","Department of Political Science, TransAtlantic Masters Program","Master of Arts in Political Science, TransAtlantic Masters","政治学跨大西洋双硕士（TAM MA）","MA","https://catalog.unc.edu/graduate/schools-departments/political-science/","soc","details/unc-polisci-tam-ma.html"]',
]

# 1) PROGRAMS：在末条 unc-ese-ms 之后、`\n];` 之前插入
prog_anchor = 'url:"details/unc-ese-ms.html"},\n];'
prog_new = ',\n'.join(t[0] for t in new_progs)
prog_replacement = 'url:"details/unc-ese-ms.html"},\n' + prog_new + '\n];'
assert prog_anchor in idx, "PROGRAMS anchor not found"
idx = idx.replace(prog_anchor, prog_replacement, 1)

# 2) CATD：在末条 unc-ese-ms 之后、`\n];` 之前插入（只 replace \n];，保留末条 ],）
catd_anchor = 'details/unc-ese-ms.html"],\n];'
catd_new = '\n' + ',\n'.join(new_catd)
catd_replacement = 'details/unc-ese-ms.html"],\n' + ',\n'.join(new_catd) + '\n];'
assert catd_anchor in idx, "CATD anchor not found"
idx = idx.replace(catd_anchor, catd_replacement, 1)

# 3) VERIFIED_DETAILS：在 `]);` 之前插入新 det
vd_start = idx.find("const VERIFIED_DETAILS=new Set(")
assert vd_start > 0, "VD start not found"
vd_end = idx.find("]);", vd_start)
assert vd_end > 0, "VD end not found"
new_dets = ', "details/unc-chemistry-ms.html", "details/unc-hpm-msph.html", "details/unc-epi-ms.html", "details/unc-nutrition-ms.html", "details/unc-polisci-tam-ma.html"'
idx = idx[:vd_end] + new_dets + idx[vd_end:]

open(p, "w", encoding="utf-8").write(idx)
print("APPENDED OK")
