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

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

# 新条目
new_entries = [
    {
        "id": "gatech-international-security-ms",
        "s": "gatech", "n": "国际安全硕士（MS International Security）",
        "en": "Master of Science in International Security",
        "dept": "Sam Nunn School of International Affairs", "college": "Georgia Tech",
        "disc": "soc", "lvl": "m", "deg": "MS",
        "dur": "30 学分；核心 9cr + 方向选修 12cr + 自由选修 9cr",
        "stem": 0, "rank": "", "test_cls": "",
        "ddl": "catalog 页未列具体轮次（待补）",
        "lang": "TOEFL iBT / IELTS Academic（最低分待补）",
        "tui": "catalog 页未列打包学费（待补）",
        "url": "details/gatech-international-security-ms.html",
    },
    {
        "id": "gatech-mbid",
        "s": "gatech", "n": "生物医学创新与发展硕士（MBID）",
        "en": "Master of Biomedical Innovation and Development",
        "dept": "Wallace H. Coulter Department of Biomedical Engineering", "college": "Georgia Tech",
        "disc": "eng", "lvl": "m", "deg": "MBID",
        "dur": "30 学分；9 门核心课含 Team Masters Project 9cr",
        "stem": 1, "rank": "", "test_cls": "",
        "ddl": "catalog 页未列具体轮次（待补）",
        "lang": "TOEFL iBT / IELTS Academic（最低分待补）",
        "tui": "catalog 页未列打包学费（待补）",
        "url": "details/gatech-mbid.html",
    },
    {
        "id": "gatech-pmase",
        "s": "gatech", "n": "应用系统工程专业硕士（PMASE）",
        "en": "Professional Master's in Applied Systems Engineering",
        "dept": "跨学院（ECE/ME/ISyE）", "college": "Georgia Tech",
        "disc": "eng", "lvl": "m", "deg": "PMASE",
        "dur": "30 学分；核心 24cr + 方向选修 3cr + Leadership 3cr",
        "stem": 1, "rank": "", "test_cls": "",
        "ddl": "catalog 页未列具体轮次（待补）",
        "lang": "TOEFL iBT / IELTS Academic（最低分待补）",
        "tui": "catalog 页未列打包学费（待补）",
        "url": "details/gatech-pmase.html",
    },
    {
        "id": "gatech-mred",
        "s": "gatech", "n": "房地产开发硕士（MRED）",
        "en": "Master of Real Estate Development",
        "dept": "College of Design（BCFM/ARCH/CP）跨学院", "college": "Georgia Tech",
        "disc": "biz", "lvl": "m", "deg": "MRED",
        "dur": "学制约 30 学分；核心 18cr + Capstone 3cr + 选修",
        "stem": 0, "rank": "", "test_cls": "",
        "ddl": "catalog 页未列具体轮次（待补）",
        "lang": "TOEFL iBT / IELTS Academic（最低分待补）",
        "tui": "catalog 页未列打包学费（待补）",
        "url": "details/gatech-mred.html",
    },
    {
        "id": "gatech-ms-architecture",
        "s": "gatech", "n": "建筑学研究型硕士（MS Architecture）",
        "en": "Master of Science in Architecture",
        "dept": "College of Architecture", "college": "Georgia Tech",
        "disc": "art", "lvl": "m", "deg": "MS",
        "dur": "30 学分；含 Master's Thesis；研究型",
        "stem": 0, "rank": "", "test_cls": "",
        "ddl": "catalog 页未列具体轮次（待补）",
        "lang": "TOEFL iBT / IELTS Academic（最低分待补）",
        "tui": "catalog 页未列打包学费（待补）",
        "url": "details/gatech-ms-architecture.html",
    },
]

# 检查重复（只看 PROGRAMS 段）
prog_seg = idx[idx.find("const PROGRAMS="):idx.find("const CATD=")]
for e in new_entries:
    if 'id:"%s"' % e["id"] in prog_seg:
        print("SKIP dup in PROGRAMS:", e["id"])
        sys.exit(1)
    # det 是否已在 PROGRAMS 段
    if 'url:"%s"' % e["url"] in prog_seg:
        print("SKIP dup url in PROGRAMS:", e["id"], e["url"])
        sys.exit(1)

# 构造新条目字符串
entries_str = ""
for e in new_entries:
    entries_str += '\n{id:"%s",s:"%s",n:"%s",en:"%s",dept:"%s",college:"%s",disc:"%s",lvl:"%s",deg:"%s",dur:"%s",stem:%d,rank:"%s",test:{cls:"%s"},ddl:"%s",lang:"%s",tui:"%s",url:"%s"},' % (
        e["id"], e["s"], e["n"], e["en"], e["dept"], e["college"],
        e["disc"], e["lvl"], e["deg"], e["dur"], e["stem"], e["rank"], e["test_cls"],
        e["ddl"], e["lang"], e["tui"], e["url"])

# 找 PROGRAMS 末条锚点（gatech-esm-ms 后是 ];）
anchor = 'url:"details/gatech-esm-ms.html"},\n];'
if anchor not in idx:
    # 尝试找最后一个 gatech 条目
    m = re.search(r'\{id:"gatech-[^"]+"[^}]*\},\n\];', idx)
    if m:
        print("anchor found:", m.group(0)[:120])
        anchor = m.group(0)
    else:
        print("ANCHOR NOT FOUND")
        sys.exit(1)

# 替换：在 ]; 前插入新条目
new_anchor = anchor.replace("\n];", entries_str + "\n];")
idx2 = idx.replace(anchor, new_anchor, 1)

# VD 追加：在 VERIFIED_DETAILS 集合的 ] 前插入新 det
vd_start = idx2.find("const VERIFIED_DETAILS=new Set([")
vd_end = idx2.find("]);", vd_start)
new_dets = ', "' + '", "'.join(e["url"] for e in new_entries) + '"'
idx2 = idx2[:vd_end] + new_dets + idx2[vd_end:]

# CATD 补录（新 det 若 CATD 缺失）
# 先检查
catd_seg = idx2[idx2.find("const CATD="):idx2.find("const VERIFIED_DETAILS=")]
catd_add = ""
for e in new_entries:
    if e["url"] not in catd_seg:
        catd_add += '\n["%s","%s","%s","%s","%s","%s","%s","%s"],' % (
            e["s"], e["dept"], e["en"], e["n"], e["deg"],
            "https://catalog.gatech.edu/programs/", e["disc"], e["url"])
        print("CATD add:", e["id"])

if catd_add:
    # CATD 末条 ];
    catd_end = idx2.find("];", idx2.find("const CATD="))
    idx2 = idx2[:catd_end] + catd_add + idx2[catd_end:]

open("index_main.html", "w", encoding="utf-8").write(idx2)
print("WRITTEN. new entries:", len(new_entries))
