import re

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

new_records = [
    ('mcg-msc-nutrition-thesis', 'mcg', '人类营养学理学硕士（论文型 · MSc Thesis）', 'Master of Science in Human Nutrition (Thesis)', 'MSc', 'm', 1, '~2 years (45 credits = 12 coursework + 33 thesis)',
     'January (fall entry)', 'TOEFL iBT 86 (sublist 20) / IELTS 6.5 (sublist 6.0)', 'GRE not required; min CGPA 3.3/4.0; undergrad nutrition/food science/biochem/medicine background',
     '~CAD 5,000-10,000/year (research funding/TA)', 'details/mcg-msc-nutrition-thesis.html'),
    ('harvard-mpp', 'harvard', '肯尼迪政府学院公共政策硕士（2 年 · MPP）', 'Master in Public Policy (MPP)', 'MPP', 'm', 0, '2 years full-time on-campus',
     'Dec 1, 2026 (12:00 ET)', 'TOEFL iBT 100 / IELTS 7.0', 'GRE/GMAT required, waivable with B+ intermediate econ + stats/calculus; bachelor direct entry, avg 2-3 yrs work exp',
     '$68,902/year (2025-2026)', 'details/harvard-mpp.html'),
    ('ubc-masc-civil', 'ubc', '土木工程应用科学硕士（论文型 · MASc）', 'Master of Applied Science in Civil Engineering (MASc)', 'MASc', 'm', 1, '16-24 months (30 credits = 18 coursework + 12 thesis)',
     'Jan 15, 2027 (fall priority)', 'TOEFL iBT 100 (R22 L22 S21 W25) / IELTS 7.0 (sublist 6.5)', 'GRE optional; min GPA B+ (76%); 4-year civil engineering bachelor',
     '~CAD 10,000-15,000/year (supervisor funding/TA)', 'details/ubc-masc-civil.html'),
    ('dartmouth-mph-campus', 'dartmouth', '公共卫生硕士（11 个月在校加速版 · MPH）', 'Master of Public Health (MPH, Accelerated On-Campus)', 'MPH', 'm', 0, '11 months on-campus (4 terms, graduate next June)',
     'SOPHAS rolling (recommend Jan)', 'TOEFL iBT 100 / IELTS 7.0 / Duolingo 130', 'GRE not required/reviewed for MPH; bachelor direct entry, no work exp required; CEPH accredited',
     '~$70,000/year (待补)', 'details/dartmouth-mph-campus.html'),
    ('mit-tpp', 'mit', '技术与政策理学硕士（SM · IDSS）', 'Master of Science in Technology and Policy (TPP)', 'SM', 'm', 1, '4 terms (~2 years) with research thesis',
     'Dec 15, 2026 (fall entry)', 'IELTS 7.5 (TOEFL not accepted) / DET 135 / C1 Advanced 191', 'GRE optional (2026-2027 cycle); strong engineering/science background; avg 1+ yr work/research exp',
     '~$60,000+/year (RA/funding 待补)', 'details/mit-tpp.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)})')
