# -*- coding: utf-8 -*-
"""r365 回填：7 条 Waterloo 博士（生物/化学/化工/机械/BME/会计/物理）"""
import io, re, shutil, os

HTML = 'index_main.html'

PHD = ('2026-27 秋季国际生费率（Finance 费率表）：博士研究型 $7,396.00/学期、杂费 $1,014.11/学期（含 HST）；'
       '兼读 $3,698.00 + $693.99（兼读杂费 $693.99）；PhD 最低资助 $29,145/年（2026-05-01 起、最多 4 年），PGS 至 $10,000；'
       '按学期计费、费率每年由 Board of Governors 批准；项目打包总额官网未公布，不估算')
PHD_CHEME = ('2026-27 秋季国际生费率（Finance 费率表）：博士研究型 $7,396.00/学期、杂费 $1,022.59/学期（化工系独立行，含 HST；兼读杂费 $702.47）；'
             '兼读学费 $3,698.00 + $693.99 杂费；PhD 最低资助 $29,145/年（2026-05-01 起、最多 4 年）；按学期计费，打包总额官网未公布')
PHD_MME = ('2026-27 秋季国际生费率（Finance 费率表）：博士研究型 $7,396.00/学期、杂费 $1,031.06/学期（机械系独立行，含 HST；兼读杂费 $710.94）；'
           '兼读学费 $3,698.00 + $693.99 杂费；PhD 最低资助 $29,145/年（2026-05-01 起、最多 4 年）；按学期计费，打包总额官网未公布')
PHD_BME = ('2026-27 秋季国际生费率（Finance 费率表）：博士研究型 $7,396.00/学期、杂费 $1,014.11/学期（含 HST）；兼读 $3,698.00 + $693.99；'
           '项目页注明学费与杂费「尚未公布，将与工程学院其他博士项目一致」；PhD 最低资助 $29,145/年；打包总额官网未公布')

LANG_SCI = 'TOEFL iBT 新制 4.5（写作 5、口语 5）；IELTS Academic 7.0（写作 6.5、口语 6.5）'
LANG_ENG = 'TOEFL iBT 新制 4.5（写作 4.5、口语 4、阅读 4、听力 4）；IELTS Academic 6.5（写作 6.0、口语 6.0）'
LANG_BME = 'TOEFL iBT 新制 4.5（写作 4.5、口语 4.0）；IELTS Academic 6.5（写作 6.0、口语 6.0）'
LANG_ACC = 'TOEFL iBT 新制 5（写作 5、口语 5）；IELTS Academic 7.5（写作 7.0、口语 7.0）'

FIELDS = {
 'uw-biology-phd': {
   'n': '生物学博士（研究型 · 五个研究领域）',
   'en': 'PhD in Biology, University of Waterloo',
   'ddl': 'Fall（9 月）2 月 1 日、Winter（次年 1 月）7 月 1 日、Spring（次年 5 月）10 月 1 日'
          '（表原文未标年份；2027 申请季对应 2027-02-01 / 2027-07-01 / 2027-10-01）',
   'tui': PHD,
   'lang': LANG_SCI,
   'dur': '48 个月全日制 · 三季入学 · Thesis 研究型 · 可兼读 · 本校硕士可逐案转博',
   'test': 'notreq',
 },
 'uw-chem-phd': {
   'n': '化学博士（研究型 · 八个研究领域）',
   'en': 'PhD in Chemistry, University of Waterloo',
   'ddl': 'Fall（9 月）2 月 1 日、Winter（次年 1 月）6 月 1 日、Spring（次年 5 月）10 月 1 日'
          '（表原文未标年份；2027 申请季对应 2027-02-01 / 2027-06-01 / 2027-10-01）',
   'tui': PHD,
   'lang': LANG_SCI,
   'dur': '48 个月全日制 · 三季入学 · Thesis 研究型 · 可兼读 · 本科 89% 可直博（课程少一门）',
   'test': 'notreq',
 },
 'uw-chemeng-phd': {
   'n': '化学工程博士（研究型 · 六个研究领域）',
   'en': 'PhD in Chemical Engineering, University of Waterloo',
   'ddl': 'Fall（9 月）2 月 1 日、Winter（次年 1 月）6 月 1 日、Spring（次年 5 月）10 月 1 日'
          '（表原文未标年份；2027 申请季对应 2027-02-01 / 2027-06-01 / 2027-10-01）'
          '——全部材料须在提交后 10 个工作日内上传',
   'tui': PHD_CHEME,
   'lang': LANG_ENG,
   'dur': '48 个月全日制 · 三季入学 · Thesis 研究型 · 可兼读 · 录取时须有已书面同意的 ADDS 导师',
   'test': 'notreq',
 },
 'uw-mme-phd-mse': {
   'en': 'PhD in Mechanical and Mechatronics Engineering (Materials Engineering and Processing field), University of Waterloo',
   'ddl': 'Fall（9 月）2 月（原文未标具体日）、Winter（次年 1 月）6 月 1 日、Spring（次年 5 月）10 月 1 日'
          '（表原文未标年份；2027 申请季对应 2027-02-01（原文缺日，按同院惯例）/ 2027-06-01 / 2027-10-01）',
   'tui': PHD_MME,
   'lang': LANG_ENG,
   'dur': '48 个月全日制 · 三季入学 · Thesis 研究型 · 可兼读 · 五个研究方向（含材料工程与加工）',
   'test': 'notreq',
 },
 'uw-bme-phd': {
   'ddl': 'Fall（9 月）2 月 1 日、Winter（次年 1 月）6 月 1 日、Spring（次年 5 月）10 月 1 日'
          '（表原文未标年份；2027 申请季对应 2027-02-01 / 2027-06-01 / 2027-10-01）',
   'tui': PHD_BME,
   'lang': LANG_BME,
   'dur': '48 个月全日制 · 三季入学 · Thesis 研究型 · 仅全日制 · 本科 BME 80% 可直博',
   'test': 'notreq',
 },
 'uw-accounting-phd': {
   'ddl': 'Fall（次年 9 月）入学，全年仅此一轮：12 月 1 日截止（表原文未标年份；2027 申请季对应 2026-12-01）'
          '——原文只列 12 月 1 日，旧记录「1 月 15 日（本地生）」未见于项目页，已按官网更正',
   'tui': PHD,
   'lang': LANG_ACC,
   'dur': '48 个月全日制 · 仅秋季入学 · Thesis 研究型 · 仅全日制 · 导师在第一学年末分配',
   'test': 'req',
 },
 'uw-physics-phd': {
   'en': 'PhD in Physics, University of Waterloo',
   'ddl': 'Fall（9 月）1 月 9 日、Winter（次年 1 月）6 月 1 日、Spring（次年 5 月）10 月 1 日'
          '（表原文未标年份；2027 申请季对应 2027-01-09 / 2027-06-01 / 2027-10-01）'
          '——秋季线早于理学院生物（2 月 1 日）与化学（2 月 1 日）',
   'tui': PHD,
   'lang': LANG_SCI,
   'dur': '48 个月全日制 · 三季入学 · Thesis 研究型 · 可兼读 · 资助覆盖学费与部分生活费',
   'test': 'notreq',
 },
}

html = io.open(HTML, encoding='utf-8').read()
if not os.path.exists(HTML + '.bak_r365'):
    shutil.copy(HTML, HTML + '.bak_r365')

start = html.index('const PROGRAMS=[')
end = html.index('const CATD=[')
seg = html[start:end]

parts = seg.split('{id:"')
out = [parts[0]]
cnt = 0
for p in parts[1:]:
    pid = p.split('"', 1)[0]
    if pid in FIELDS:
        for k, v in FIELDS[pid].items():
            if k == 'test':
                tpat = re.compile(r'test:\{cls:"((?:[^"\\]|\\.)*)"\}')
                tms = list(tpat.finditer(p))
                assert len(tms) == 1, 'test in %s found %d' % (pid, len(tms))
                p = tpat.sub(lambda m: 'test:{cls:"' + v + '"}', p, count=1)
                cnt += 1
                continue
            pat = re.compile(r'(?<![A-Za-z])' + k + r':"((?:[^"\\]|\\.)*)"')
            ms = list(pat.finditer(p))
            assert len(ms) == 1, 'field %s in %s found %d' % (k, pid, len(ms))
            p = pat.sub(lambda m: k + ':"' + v.replace('"', '\\"') + '"', p, count=1)
            cnt += 1
    out.append(p)

new_seg = '{id:"'.join(out)
new_html = html[:start] + new_seg + html[end:]
assert new_html != html
io.open(HTML, 'w', encoding='utf-8', newline='').write(new_html)
print('fields', cnt, 'bytes', len(new_html.encode('utf-8')))
