import re, os, json

base = os.path.dirname(os.path.abspath(__file__))
p = os.path.join(base, "index_main.html")
html = open(p, encoding="utf-8").read()

seg = re.search(r"const PROGRAMS\s*=\s*\[(.*?)\];", html, re.S).group(1)
blocks = re.findall(r"\{id:\"([^\"]+)\"([\s\S]*?)(?=\{id:\"|$)", seg)
bmap = {bid: body for bid, body in blocks}
vd = set(re.findall(r'"(details/[^"]+\.html)"', re.search(r"const VERIFIED_DETAILS[^;]+;", html).group(0)))

CHECK = {
    "ut-mmpa": ("管理与专业会计硕士（MMPA）", "details/ut-mmpa.html"),
    "usc-aeec-ms": ("应用经济学与计量经济学硕士（MS）", "details/usc-aeec-ms.html"),
    "illinois-mspe": ("政策经济学硕士（MS）", "details/illinois-mspe.html"),
    "usc-seda-ms": ("空间经济与数据分析硕士（MS, SEDA）", "details/usc-seda-ms.html"),
    "berkeley-macss": ("计算社会科学硕士（MaCSS）", "details/berkeley-macss.html"),
    "ut-mmi": ("创新管理硕士（MMI）", "details/ut-mmi.html"),
    "ucsd-bioe-ms-med": ("生物工程硕士（MS，医疗方向）", "details/ucsd-bioe-ms-med.html"),
    "umich-aero-mse": ("航空航天工程硕士（MSE）", "details/umich-aero-mse.html"),
    "uva-msacc": ("会计学硕士（MS）", "details/uva-accounting-ms.html"),
}

for bid, (newn, url) in CHECK.items():
    body = bmap.get(bid)
    if body is None:
        print(f"!! {bid} 已不在 PROGRAMS"); continue
    n = re.search(r'n:"([^"]*)"', body).group(1)
    en = re.search(r'en:"([^"]*)"', body).group(1)
    ok_n = n == newn
    disk = os.path.exists(os.path.join(base, url))
    invd = url in vd
    print(f"{'OK' if ok_n and disk and invd else '!!'} {bid} | n={n} | disk={disk} vd={invd}")
    if bid == "uva-msacc":
        print(f"    en={en}")

# uva-msa 占位块与 VD 应已移除
print("uva-msa 块残留:", '{id:"uva-msa"' in html)
print("VD 占位页残留:", '"details/uva-msa.html"' in html)
print("占位磁盘文件保留:", os.path.exists(os.path.join(base, "details/uva-msa.html")))

# 旧英文串残留
OLD = [
    'n:"Management and Professional Accounting MMPA"',
    'n:"Applied Economics and Econometrics (MS)"',
    'n:"Policy Economics (MS)"',
    'n:"Spatial Economics and Data Analysis (MS, SEDA)"',
    'n:"Master of Computational Social Science (MaCSS)"',
    'n:"Management of Innovation MMI"',
    'n:"Bioengineering with Medical Specialization (MS)"',
    'n:"Aerospace Engineering MSE"',
]
for o in OLD:
    if o in html:
        print("!! 旧串残留:", o)
print("旧串检查完成")
