# -*- coding: utf-8 -*-
import io,sys,re,os
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding="utf-8")
p="index_main.html"
html=open(p,encoding="utf-8").read()
orig=html

DET="details/duke-cbd0bbe.html"
PID="duke-cee-ms"

# 1) PROGRAMS 幂等判断（基于原始 html）
if ('{id:"'+PID+'"') in html:
    print("PROGRAMS 已存在", PID, "跳过追加")
else:
    entry = (
        '{id:"duke-cee-ms",s:"duke",n:"土木与环境工程硕士（MS · 研究导向，论文/项目双选）",'
        'en:"Master of Science in Civil and Environmental Engineering",'
        'dept:"土木与环境工程系（CEE）· Pratt 工程学院",'
        'college:"Duke University · Pratt School of Engineering / Graduate School",disc:"eng",lvl:"m",deg:"MS",'
        'dur:"研究导向 MS；30 学分（系内课程 6 + 方向课 9 + 研究生选修 9 + 研究 6）；至少三个全日制学期；Thesis 或 Research Project 双选；仅秋季入学",'
        'stem:0,'
        'rank:"系内 Faculty 21、MS 学生 60（规模/分布非门槛）；Class of 2025 MS 口径 89% 在本领域工作或继续深造（系级去向，非承诺非门槛）",'
        'test:{cls:"opt"},'
        'ddl:"仅秋季（Fall 2027）；优先截止 2027-01-10（保证审阅）；最终截止 2027-02-25（按名额余量）",'
        'lang:"首语言非英语须交；DET 首选，可交 TOEFL/IELTS；TOEFL iBT 最低 90；IELTS/DET 最低分未公布；符合条件可豁免；录取入学后若无豁免仍须交 DET；TOEFL 机构代码 5156（无系代码）",'
        'tui:"GS 通用硕士 2026–2027 估算：学费 $34,940/学期，至少三全日制学期，之后 $3,882/学分；国内 9 个月 COA 约 $101,996、国际约 $101,780（GS 通用口径，非 CEE 专属打包价，费率每年调整）；CEE 系奖学金通常 $20,000–$30,000；申请费 US$75（Pratt 通道，可 fee waiver）",'
        'url:"details/duke-cbd0bbe.html"},\n'
    )
    anchor='url:"details/duke-c5d718f.html"},\n];'
    assert anchor in html, "PROGRAMS 锚点未找到"
    html=html.replace(anchor, 'url:"details/duke-c5d718f.html"},\n'+entry+'];', 1)
    print("PROGRAMS 追加", PID)

# 2) VD 幂等判断（限定 VERIFIED_DETAILS 段，基于原始 html）
m=re.search(r'const VERIFIED_DETAILS=new Set\(\[(.*?)\]\);', orig, re.S)
assert m, "VD 段未找到"
vd_set=m.group(1)
if ('"'+DET+'"') in vd_set:
    print("VD 已登记", DET, "跳过")
else:
    vd_anchor='"details/duke-c5d718f.html",'
    assert vd_anchor in html, "VD 锚点未找到"
    html=html.replace(vd_anchor, vd_anchor+'"'+DET+'",', 1)
    print("VD 登记", DET)

if html!=orig:
    open(p,"w",encoding="utf-8").write(html)
    print("写回完成，字节", len(orig.encode("utf-8")), "->", len(html.encode("utf-8")))
else:
    print("无变更")
