import re

h = open('index_main.html', encoding='utf-8').read()

new_records = [
    ('caltech-ms-space', 'caltech', '空间工程理学硕士（GALCIT · 1 年 · 无 thesis · terminal MS）', 'Master of Science in Space Engineering (GALCIT)', 'MS', 'm', 1, '1 year (135 units)',
     '申请窗口 12/1 - 1/15，Fall 入学', 'TOEFL / IELTS / PTE / Duolingo（无最低分）', 'GRE Recommended（非强制）；3 推荐信；无 GPA 最低要求；优先给计划读 PhD 的学生',
     'USD 71,022/年 (2026-27)', 'details/caltech-ms-space.html'),
    ('uchicago-harris-ma', 'uchicago', '公共政策文学硕士（MA · 1 年加速 · 9 门课 · Harris 学院）', 'Master of Arts in Public Policy (MA, Harris School of Public Policy)', 'MA', 'm', 0, '1 year (3 quarters, 9 courses)',
     'Autumn Quarter 入学（精确轮次待补）', 'TOEFL iBT / IELTS Academic（具体分数待补）', 'GRE required 或 waiver；9 门课 = 5 core + 4 elective；至少 7 门 PPHA；Fall 3 门 core + Winter 2 门 core',
     '约 USD 70,000+/年（精确待补）', 'details/uchicago-harris-ma.html'),
    ('dartmouth-ms-cs', 'dartmouth', '计算机科学理学硕士（MSCS · 13 门课 · Guarini 研究生院 · 在校）', 'Master of Science in Computer Science (MSCS, Guarini School)', 'MS', 'm', 1, '1.5-2 years typical / 13 courses',
     'Fall 2027: December 15, 2026', 'TOEFL iBT / IELTS / Duolingo（ETS code 3351）', 'GRE Not accepted；3 推荐信（最多 4 封）；上传 100-line code sample + CS paper sample；Digital Arts track 需 portfolio',
     '约 USD 60,000+/年（精确待补）', 'details/dartmouth-ms-cs.html'),
    ('uchicago-crown-ssa-ma', 'uchicago', '社会工作社会政策与社会行政文学硕士（MA SSA · 2 年 · Crown Family School · 含 field placement）', 'Master of Arts in Social Work, Social Policy, and Social Administration (SSA)', 'MA', 'm', 0, '2 years full-time（含 2-year field placement）',
     'Autumn Quarter 入学（精确轮次待补）', 'TOEFL iBT 104 overall (each sub-score 26) / IELTS 7 overall (each sub-score 7)', 'GRE not required；GPA < 2.8 需 post-bachelor social work employment；BSW 可申 Advanced Standing；CSWE 认证',
     '约 USD 60,000+/年（精确待补）', 'details/uchicago-crown-ssa-ma.html'),
    ('notre-msa', 'notre', '会计理学硕士（MSA · 10 个月 · 30 学分 · Mendoza 在校 · 可选 STEM 数据 analytics 方向）', 'Master of Science in Accountancy (MSA, Mendoza College of Business)', 'MSA', 'm', 1, '10 months / 30 credit hours',
     'EA Sep 10, 2026 / R1 Oct 20, 2026 / R2 Jan 7, 2027 / R3 Mar 17, 2027', 'TOEFL / IELTS / PTE / Duolingo（具体分数待补）', 'GMAT not required for US 本科；国际生需 GMAT 或 waiver；6 门会计先修；2 推荐信（至少 1 学术）；可选 STEM Data Analytics concentration',
     'USD 55,660（10 个月总学费）', 'details/notre-msa.html'),
]

def fmt(rec):
    pid, s, n, en, deg, lvl, stem, dur, ddl, lang, test, tui, url = rec
    return (f'{{id:"{pid}",s:"{s}",n:"{n}",en:"{en}",deg:"{deg}",lvl:"{lvl}",stem:{stem},dur:"{dur}",ddl:"{ddl}",lang:"{lang}",test:{{cls:"{test}"}},tui:"{tui}",url:"{url}"}}')

new_block = ',\n' + ',\n'.join(fmt(r) for r in new_records)

m = re.search(r'const PROGRAMS\s*=\s*\[', h)
if not m:
    raise SystemExit('PROGRAMS start not found')
start = m.end()
depth = 1
i = start
in_str = False
str_ch = ''
while i < len(h) and depth > 0:
    c = h[i]
    if in_str:
        if c == '\\':
            i += 2
            continue
        if c == str_ch:
            in_str = False
    else:
        if c in ('"', "'"):
            in_str = True
            str_ch = c
        elif c == '[':
            depth += 1
        elif c == ']':
            depth -= 1
    i += 1
end_bracket = i - 1
new_h = h[:end_bracket] + new_block + '\n' + h[end_bracket:]

m2 = re.search(r'VERIFIED_DETAILS\s*=\s*new\s*Set\s*\(', new_h)
if not m2:
    raise SystemExit('VD Set start not found')
paren_start = m2.end()
depth = 1
j = paren_start
in_str = False
str_ch = ''
while j < len(new_h) and depth > 0:
    c = new_h[j]
    if in_str:
        if c == '\\':
            j += 2
            continue
        if c == str_ch:
            in_str = False
    else:
        if c in ('"', "'"):
            in_str = True
            str_ch = c
        elif c == '(':
            depth += 1
        elif c == ')':
            depth -= 1
    j += 1
paren_end = j - 1

arr_depth = 0
k = paren_start
in_str = False
str_ch = ''
arr_close = -1
while k < paren_end:
    c = new_h[k]
    if in_str:
        if c == '\\':
            k += 2
            continue
        if c == str_ch:
            in_str = False
    else:
        if c in ('"', "'"):
            in_str = True
            str_ch = c
        elif c == '[':
            arr_depth += 1
        elif c == ']':
            arr_depth -= 1
            if arr_depth == 0:
                arr_close = k
                break
    k += 1
if arr_close < 0:
    raise SystemExit('VD array close not found')

new_urls = [r[12] for r in new_records]
vd_insert = ',\n  "' + '",\n  "'.join(new_urls) + '"\n'
new_h = new_h[:arr_close] + vd_insert + new_h[arr_close:]

open('index_main.html', 'w', encoding='utf-8').write(new_h)
print(f'[OK] PROGRAMS +{len(new_records)}, VD +{len(new_urls)}')
print(f'File size: {len(new_h)} chars (was {len(h)})')
