import re, os

base = os.path.dirname(os.path.abspath(__file__))
html = open(os.path.join(base, "index_main.html"), 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 = {
    "berkeley-cbe-pdp": ("化学与生物分子工程硕士（MS，产品开发方向）", "details/berkeley-cbe-pdp.html"),
    "ucsd-se-ms": ("结构工程硕士（MS）", "details/ucsd-se-ms.html"),
    "uva-se-ms": ("系统工程硕士（MS）", "details/uva-se-ms.html"),
    "ubc-mppga": ("公共政策与全球事务硕士（MPPGA）", "details/ubc-mppga.html"),
    "usc-hsme-ms": ("健康系统管理工程硕士（MS）", "details/usc-hsme-ms.html"),
    "usc-mnlm": ("非营利组织领导与管理硕士（MNLM）", "details/usc-mnlm.html"),
    "ut-c7a2833": ("可持续管理硕士（MScSM）", "details/ut-c7a2833.html"),
    "usc-me-emgmt-dual-ms": ("机械工程与工程管理双学位硕士", "details/usc-me-emgmt-dual-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)
    disk = os.path.exists(os.path.join(base, url))
    print(f"{'OK' if n == newn and disk and url in vd else '!!'} {bid} | n={n} | disk={disk} vd={url in vd}")

# 占位块与 VD 应已移除
for bid, url in [("ut-mscsm", "details/ut-mscsm.html"), ("usc-me-em-dual", "details/usc-me-em-dual.html")]:
    print(f"{bid} 块残留: {('{id:\"'+bid+'\"') in html} | VD 残留: {url in vd} | 磁盘保留: {os.path.exists(os.path.join(base, url))}")

# 旧英文串残留
OLD = [
    'n:"Chemical Biomolecular Engineering Professional MS"',
    'n:"Structural Engineering MS"',
    'n:"Systems Engineering MS"',
    'n:"Public Policy and Global Affairs (MPPGA)"',
    'n:"Health Systems Management Engineering (MS)"',
    'n:"Nonprofit Leadership and Management (MNLM)"',
]
for o in OLD:
    if o in html:
        print("!! 旧串残留:", o)
print("旧串检查完成")
