# -*- coding: utf-8 -*-
import io, sys, re
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/cornell-c10e6fd.html"
PID = "cornell-is-ms"
LAST_DET = "details/cornell-ce9af91.html"

if ('{id:"' + PID + '"') in html:
    print("PROGRAMS 已存在", PID, "跳过追加")
else:
    entry = (
        '{id:"cornell-is-ms",s:"cornell",n:"信息系统双硕士（Jacobs Technion-Cornell Dual M.S. · 纽约校区）",'
        'en:"Master of Science in Information Systems",'
        'dept:"Jacobs Technion-Cornell Institute · Cornell Tech × Technion",'
        'college:"Cornell Tech · New York City, NY",disc:"cs",lvl:"m",deg:"MS",'
        'dur:"全日制纽约在校；Programmatic Core 17 + Technical Core 10 + Specialization Project 8（跨两学期）+ General Electives 12 + 方向 Core 9 / Electives 12",'
        'stem:0,'
        'rank:"官网未公布项目级最低 GPA、录取 GPA 分布、班级规模、录取率与平均薪资，相关字段留空不估算",'
        'test:{cls:"notreq"},'
        'ddl:"2027 秋入学：Priority 1/5 17:00 EST；Round 2/2 17:00 EST；Rolling 4/1 17:00 EST；逾期顺延下一轮，奖学金评审建议 Priority 轮前提交",'
        'lang:"TOEFL iBT 总分 ≥100；或 IELTS Academic 总分 ≥7.0；不接受 MyBestScore；豁免按 Cornell Graduate 口径（英语国家全日制学习/工作满两年且连续、10 年内）",'
        'tui:"项目级学费费率与总额官网未单列留空不估算；有限名额硕士奖学金按综合评审、录取时评定，Priority 轮前申请优先",'
        'url:"details/cornell-c10e6fd.html"},\n'
    )
    anchor = 'url:"' + LAST_DET + '"},\n];'
    assert anchor in html, "PROGRAMS 锚点未找到"
    html = html.replace(anchor, 'url:"' + LAST_DET + '"},\n' + entry + '];', 1)
    print("PROGRAMS 追加", PID)

vm = re.search(r'(const VERIFIED_DETAILS=new Set\(\[)(.*?)(\]\);)', html, re.S)
assert vm, "VD 段未找到"
vd_body = vm.group(2)
if ('"' + DET + '"') in vd_body:
    print("VD 段已登记", DET, "跳过")
else:
    vd_anchor = '"' + LAST_DET + '"]);'
    assert vd_anchor in html, "VD 段末锚点未找到"
    vd_body_new = vd_body.replace('"' + LAST_DET + '"',
                                   '"' + LAST_DET + '","' + DET + '"', 1)
    html = html[:vm.start(2)] + vd_body_new + html[vm.end(2):]
    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("无变更")
