# -*- coding: utf-8 -*-
import io, sys, os, re
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
BASE = r"D:\work buddy\2026-11-05-14-15-52"
F = os.path.join(BASE, "index_main.html")
html = open(F, encoding="utf-8").read()
orig = html

NEW_ID = "notre-acms-ms"
NEW_DET = "details/notre-acms.html"

# 1) 幂等：已存在则跳过 PROGRAMS
if ('id:"' + NEW_ID + '"') in html:
    print("PROGRAMS 已存在", NEW_ID, "跳过追加")
else:
    entry = (
        '{id:"' + NEW_ID + '",s:"notre",n:"应用计算数学与统计硕士（专业型）",'
        'en:"Master of Science in Applied and Computational Mathematics and Statistics (MS-ACMS, Professional)",'
        'dept:"应用与计算数学及统计系（ACMS）· 理学院",'
        'college:"College of Science, University of Notre Dame",'
        'disc:"cs",lvl:"m",deg:"MS",'
        'dur:"10 个月连续就读，或 16 个月（第 2、3 学期间暑期可实习）；全日制在校；30 学分；最低一学期在校",'
        'stem:0,rank:"2027 USNews 第 20", test:{cls:"opt"},'
        'ddl:"1 月 15 日；晚申请接受至 5 月 31 日（官网未标年份；按 2027 申请季：2027 秋 2027-01-15，晚申至 2027-05-31）；晚于截止需联系项目主任",'
        'lang:"TOEFL / IELTS / Duolingo（非英语母语；项目页未公布最低总分/小分门槛）",'
        'tui:"项目页与 ACMS 系站未公布项目级学费/杂费费率与总额，留空不估算；专业型硕士项目级资助名额/金额官网未单列",'
        'url:"' + NEW_DET + '"},'
    )
    anchor = 'url:"details/notre-mscse.html"},\n];'
    if anchor not in html:
        print("ERR: 末条锚点未找到，未写回"); sys.exit(1)
    html = html.replace(anchor, 'url:"details/notre-mscse.html"},\n' + entry + '\n];', 1)
    print("PROGRAMS 已追加", NEW_ID)

# 2) VD 登记：基于原始 html 判断（避免内存已插入误判）
if NEW_DET not in orig:
    vd_anchor = 'const VERIFIED_DETAILS=new Set(["details/notre-mscse.html",'
    if vd_anchor not in html:
        print("ERR: VD 锚点未找到"); sys.exit(1)
    html = html.replace(vd_anchor,
        'const VERIFIED_DETAILS=new Set(["' + NEW_DET + '","details/notre-mscse.html",', 1)
    print("VD 已登记", NEW_DET)
else:
    print("VD 已含", NEW_DET, "跳过")

if html != orig:
    open(F, "w", encoding="utf-8", newline="").write(html)
    print("主文件已写回，字节", len(orig), "->", len(html))
else:
    print("无改动")
