# -*- 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-msee"
NEW_DET = "details/notre-msee.html"

# 1) 幂等：已存在则跳过 PROGRAMS
if ('id:"' + NEW_ID + '"') in html:
    print("PROGRAMS 已存在", NEW_ID, "跳过追加")
else:
    entry = (
        '{id:"' + NEW_ID + '",s:"notre",n:"电气工程硕士（MSEE）",'
        'en:"Master of Science in Electrical Engineering (MSEE)",'
        'dept:"电气工程系（EE）· 工程学院",'
        'college:"College of Engineering, University of Notre Dame",'
        'disc:"eng",lvl:"m",deg:"MS",'
        'dur:"典型 1–1.5 年；全日制在校；30 学分；非论文型 ≥27 讲座课学分，论文型 ≥18 讲座课学分 + 6 论文指导学分；仅秋季入学",'
        'stem:0,rank:"2027 USNews 第 20", test:{cls:"rec"},'
        'ddl:"仅秋季（Fall）入学；Fall 截止 12 月 15 日（官网列月份日期未标年份；按 2027 申请季：2027 秋 2026-12-15）",'
        'lang:"TOEFL iBT 80（口语 23；口语低于 23 可能安排电话/视频面试）；IELTS 7.0；英语教学院校满两学年可豁免；Duolingo 不推荐替代 TOEFL/IELTS",'
        'tui:"学生自付学费；系/学院对 MSEE 无部门或学院资助；项目级学费费率/总额官网未公布，留空不估算",'
        'url:"' + NEW_DET + '"},'
    )
    anchor = 'url:"details/notre-acms.html"},\n];'
    if anchor not in html:
        print("ERR: 末条锚点未找到，未写回"); sys.exit(1)
    html = html.replace(anchor, 'url:"details/notre-acms.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-acms.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-acms.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("无改动")
