import re

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

new_records = [
    ('cornell-mps-acct', 'cornell', '管理学专业研究硕士（会计方向）（MPS in Management – Accounting · 10 个月 · STEM · Johnson）',
     'Master of Professional Studies (MPS) in Management – Accounting Specialization, Samuel Curtis Johnson Graduate School of Management',
     'MPS', 'm', 1, '10 months, full-time residential (Ithaca, NY) - 准备 CPA 考试',
     'Fall（8 月）入学；具体轮次待补',
     'TOEFL iBT / IELTS Academic / Duolingo（具体分数待补；不接受 TOEFL MyBest Score）',
     'GMAT/GRE required（录取分布非最低门槛）；外部学生需先修 8 门课',
     '约 USD 70,000+/10-month（精确待补）',
     'details/cornell-mps-acct.html'),
    ('unc-mac', 'unc', '会计硕士（MAC · 10-12 个月 · On-Campus · Kenan-Flagler）',
     'Master of Accounting (MAC, On-Campus, Kenan-Flagler Business School)',
     'MAC', 'm', 0, '10-12 months, full-time residential (June start, 也有 early August accelerated option) - 48 credits',
     'June 入学（每年 1 次）；Aug accelerated option 需 3 门先修课 B+',
     'TOEFL iBT / IELTS Academic（具体分数待补）',
     'GRE/GMAT 政策待补（Holistic review）；面向 non-accounting 本科',
     'in-state $46,076 / out-of-state $67,413（学费 + fees，12-month on-campus）',
     'details/unc-mac.html'),
    ('uva-ms-commerce', 'uva', '商学硕士（MS in Commerce · 10 个月 · 4 tracks · STEM · McIntire）',
     'Master of Science in Commerce (M.S. in Commerce, McIntire School of Commerce)',
     'MSC', 'm', 1, '10 months, full-time residential (Charlottesville, VA) - 40 credit hours',
     'Fall 入学；5 轮申请（具体轮次待补）',
     'TOEFL iBT / IELTS Academic（具体分数待补）',
     'GRE/GMAT 政策待补（Holistic review）；商科本科不可申请；毕业不超过 18 个月内',
     '约 USD 70,000+/10-month（精确待补）',
     'details/uva-ms-commerce.html'),
    ('nyu-msacc', 'nyu', '会计学硕士（MS in Accounting · 1 年 · 30 学分 · CPA-ready · Stern）',
     'Master of Science in Accounting (MS in Accounting, Stern School of Business)',
     'MSAcc', 'm', 0, '1 year, full-time on-campus (Greenwich Village, NYC) - 30 credits',
     'Fall 入学；具体轮次待补',
     'TOEFL iBT / IELTS Academic（具体分数待补）',
     'GRE/GMAT 政策待补（Holistic review）；满足 NY State CPA licensure 教育要求',
     '约 USD 80,000+/year（精确待补）',
     'details/nyu-msacc.html'),
    ('jhu-msf', 'jhu', '金融学硕士（MS in Finance · 9/12/15 个月 · STEM · CFA-aligned · Carey）',
     'Master of Science in Finance (MS in Finance, Carey Business School) - 9/12/15-month full-time formats',
     'MSF', 'm', 1, '9 months (30 credits, 2 semesters) / 12 months (36 credits, 3 semesters) / 15 months (36 credits, 4 semesters)',
     'Fall Term 入学；具体轮次待补',
     'TOEFL iBT / IELTS Academic（具体分数待补）',
     'GRE/GMAT 政策待补（Holistic review）；CFA-aligned；Course waivers are not granted',
     '9-month $78,500 / 12-month $94,500 / 15-month $99,750（学费 + fees）',
     'details/jhu-msf.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)})')
