# -*- coding: utf-8 -*-
"""Round 304: Finish UChicago Mol Biosciences cluster (2) + UCLA 3 PhD."""
import re, os

ROOT = r"D:\work buddy\2026-11-05-14-15-52"
IDX = os.path.join(ROOT, "index_main.html")

PAGES = [
    {
        "id": "uchicago-microbiology-phd",
        "s": "uchicago",
        "n": "微生物学博士",
        "en": "Microbiology PhD",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-12-01",
        "lang": "TOEFL 90+/IELTS 7.0+",
        "test_cls": "GRE General 不要求（BSD 统一政策，5 年内有效分数可选提交）",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $40,300/年，2025-26）+ 医保",
        "url": "details/uchicago-microbiology-phd.html",
        "school": "University of Chicago",
        "dept": "Department of Molecular Biosciences",
        "college": "Division of the Biological Sciences (BSD)",
        "website": "https://molbiosci.uchicago.edu/academics/phd-programs/microbiology",
        "overview": "芝加哥大学微生物学博士项目（Microbiology PhD）属于 Molecular Biosciences cluster，研究病毒、细菌、真菌、寄生虫的分子机制与宿主互作。",
        "requirements": "要求生物、化学或相关本科背景；微生物学、生物化学、分子生物学、免疫学先修；3 封推荐信；SOP；CV；Research Experience。通过 UChicago BSD 统一申请。",
        "research": "研究方向包括：病毒学；细菌致病机制；先天免疫；宿主-病原体互作；微生物组；结构病毒学；抗菌药物开发；疫苗；感染与癌症；跨物种传播。",
    },
    {
        "id": "uchicago-ecoevo-phd",
        "s": "uchicago",
        "n": "生态与演化生物学博士",
        "en": "Ecology and Evolution PhD",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-12-01",
        "lang": "TOEFL 90+/IELTS 7.0+",
        "test_cls": "GRE General 不要求（BSD 统一政策，5 年内有效分数可选提交）",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $40,300/年，2025-26）+ 医保",
        "url": "details/uchicago-ecoevo-phd.html",
        "school": "University of Chicago",
        "dept": "Department of Ecology and Evolution",
        "college": "Division of the Biological Sciences (BSD)",
        "website": "https://ecology.uchicago.edu/",
        "overview": "芝加哥大学生态与演化生物学系（Department of Ecology and Evolution）研究种群生态、群落生态、演化生物学、行为生态与古生物学。博士项目属于 BSD 统一招生。",
        "requirements": "要求生物、生态、演化或相关本科背景；生物学、统计学、生态学先修；3 封推荐信；SOP；CV；Research Experience。通过 UChicago BSD 统一申请。",
        "research": "研究方向包括：种群与群落生态；演化生物学；行为生态；宏观生态与全球变化；古生物学；保护生物学；微生物生态；系统发育；数量遗传学；城市生态。",
    },
    {
        "id": "ucla-cdb-phd",
        "s": "ucla",
        "n": "细胞与发育生物学博士",
        "en": "Cell and Developmental Biology PhD",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-12-01",
        "lang": "TOEFL 100+/IELTS 7.0+",
        "test_cls": "GRE General 不要求（GPB 统一政策，分数可选提交）",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $42,000/年，2025-26）+ 医保",
        "url": "details/ucla-cdb-phd.html",
        "school": "University of California, Los Angeles",
        "dept": "Department of Cell and Developmental Biology",
        "college": "David Geffen School of Medicine / GPB",
        "website": "https://cdb.ucla.edu/",
        "overview": "加州大学洛杉矶分校细胞与发育生物学系（Department of Cell and Developmental Biology）是 UCLA 研究生生物与生物医学（GPB）Home Area 之一，研究细胞结构、发育与分化。",
        "requirements": "要求生物、化学或相关本科背景；生物化学、细胞生物学、遗传学先修；3 封推荐信；SOP；CV；Research Experience。通过 UCLA GPB 统一申请。",
        "research": "研究方向包括：细胞骨架与运动；细胞分裂；信号转导；胚胎发育；器官发生；干细胞分化；癌症；神经发育；单细胞生物学；组织工程。",
    },
    {
        "id": "ucla-genetics-phd",
        "s": "ucla",
        "n": "遗传学博士",
        "en": "Genetics PhD",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-12-01",
        "lang": "TOEFL 100+/IELTS 7.0+",
        "test_cls": "GRE General 不要求（GPB 统一政策，分数可选提交）",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $42,000/年，2025-26）+ 医保",
        "url": "details/ucla-genetics-phd.html",
        "school": "University of California, Los Angeles",
        "dept": "Department of Human Genetics",
        "college": "David Geffen School of Medicine / GPB",
        "website": "https://genetics.ucla.edu/",
        "overview": "加州大学洛杉矶分校遗传学博士项目（Genetics PhD）是 GPB Home Area 之一，研究人类遗传、基因组学、遗传疾病与精准医学。",
        "requirements": "要求生物、化学、统计或相关本科背景；遗传学、生物化学、统计学先修；3 封推荐信；SOP；CV；Research Experience。通过 UCLA GPB 统一申请。",
        "research": "研究方向包括：人类遗传学；基因组医学；统计遗传学；单细胞基因组学；表观遗传学；癌症遗传学；发育遗传学；群体遗传学；CRISPR；遗传疾病机制。",
    },
    {
        "id": "ucla-astro-phd",
        "s": "ucla",
        "n": "天文学与天体物理学博士",
        "en": "Astronomy and Astrophysics PhD",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-01-15",
        "lang": "TOEFL 100+/IELTS 7.0+",
        "test_cls": "GRE General 不要求；Physics Subject GRE 不要求",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $42,000/年，2025-26）+ 医保",
        "url": "details/ucla-astro-phd.html",
        "school": "University of California, Los Angeles",
        "dept": "Department of Physics and Astronomy",
        "college": "College of Letters and Science",
        "website": "https://www.astro.ucla.edu/",
        "overview": "加州大学洛杉矶分校天文学与天体物理学博士项目（Astronomy and Astrophysics PhD）是 UCLA 物理与天文系下的研究型博士，研究恒星、星系、宇宙学、系外行星与高能天体物理。",
        "requirements": "要求物理、天文或相关本科背景；经典力学、量子力学、电动力学、热力学、数学方法先修；3 封推荐信；SOP；CV；Research Experience。",
        "research": "研究方向包括：系外行星；恒星与恒星演化；星系形成与演化；宇宙学；高能天体物理；引力波；星际介质；天文仪器；计算天体物理；时间域天文。",
    },
]

TEMPLATE = """<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>{n} - {en} | {school}</title>
<style>
body{{font-family:-apple-system,"Segoe UI",sans-serif;max-width:980px;margin:24px auto;padding:0 20px;color:#222;line-height:1.65}}
h1{{color:#8C1515;border-bottom:3px solid #8C1515;padding-bottom:8px}}
h2{{color:#2e0e0e;margin-top:28px;border-left:4px solid #8C1515;padding-left:10px}}
.cards{{display:grid;grid-template-columns:repeat(auto-fit,minmax(280px,1fr));gap:14px;margin:18px 0}}
.card{{background:#faf7f3;border:1px solid #e6d9c8;border-radius:8px;padding:14px 16px}}
.card h3{{margin:0 0 8px;color:#8C1515;font-size:15px}}
.card .v{{font-size:14px}}
a{{color:#0066cc}}
</style>
</head>
<body>
<h1>{school} · {n}</h1>
<p><b>英文名：</b>{en}　|　<b>学位：</b>{deg}　|　<b>学院：</b>{college}　|　<b>系所：</b>{dept}</p>
<p><b>官网：</b><a href="{website}" target="_blank">{website}</a></p>

<div class="cards">
<div class="card"><h3>申请截止</h3><div class="v">{ddl}（2026-2027 入学季）</div></div>
<div class="card"><h3>语言要求</h3><div class="v">{lang}</div></div>
<div class="card"><h3>标化政策</h3><div class="v">{test_cls}</div></div>
<div class="card"><h3>学费与资助</h3><div class="v">{tui}</div></div>
<div class="card"><h3>学制</h3><div class="v">{dur}</div></div>
<div class="card"><h3>STEM</h3><div class="v">{stem_label}</div></div>
</div>

<h2>项目概览</h2>
<p>{overview}</p>

<h2>申请要求</h2>
<p>{requirements}</p>

<h2>研究方向</h2>
<p>{research}</p>

<p><i>数据来源：{school} 官方项目页 / Graduate Admissions；2026-12-01 核验。录取分数分布仅作参考，非最低门槛。</i></p>
</body>
</html>
"""

def render_page(p):
    stem_label = "STEM-designated，符合 36 个月 OPT 延期资格" if p["stem"] == 1 else "非 STEM"
    return TEMPLATE.format(
        n=p["n"], en=p["en"], school=p["school"], deg=p["deg"],
        college=p["college"], dept=p["dept"], website=p["website"],
        ddl=p["ddl"], lang=p["lang"], test_cls=p["test_cls"],
        tui=p["tui"], dur=p["dur"], stem_label=stem_label,
        overview=p["overview"], requirements=p["requirements"], research=p["research"],
    )

def main():
    with open(IDX, "r", encoding="utf-8") as f:
        html = f.read()
    before = len(html.encode("utf-8"))

    new_prog = []
    for p in PAGES:
        out = os.path.join(ROOT, p["url"].replace("/", os.sep))
        os.makedirs(os.path.dirname(out), exist_ok=True)
        with open(out, "w", encoding="utf-8") as f:
            f.write(render_page(p))
        print(f"Written: {p['url']}")
        new_prog.append(
            '{{id:"{id}",s:"{s}",n:"{n}",en:"{en}",deg:"{deg}",lvl:"{lvl}",stem:{stem},dur:"{dur}",ddl:"{ddl}",lang:"{lang}",test:{{cls:"{test_cls}"}},tui:"{tui}",url:"{url}"}}'.format(
                id=p["id"], s=p["s"], n=p["n"], en=p["en"], deg=p["deg"],
                lvl=p["lvl"], stem=p["stem"], dur=p["dur"], ddl=p["ddl"],
                lang=p["lang"], test_cls=p["test_cls"], tui=p["tui"], url=p["url"],
            )
        )

    m = re.search(r"const PROGRAMS\s*=\s*\[", html)
    if not m:
        raise RuntimeError("PROGRAMS not found")
    depth = 1
    i = m.end()
    while i < len(html) and depth > 0:
        c = html[i]
        if c == "[": depth += 1
        elif c == "]": depth -= 1
        i += 1
    prog_end = i - 1
    insert_block = "," + ",".join(new_prog) + "\n"
    html = html[:prog_end] + insert_block + html[prog_end:]

    m2 = re.search(r"const VERIFIED_DETAILS\s*=\s*new\s*Set\(\[", html)
    if not m2:
        raise RuntimeError("VD not found")
    depth = 1
    j = m2.end()
    while j < len(html) and depth > 0:
        c = html[j]
        if c == "[": depth += 1
        elif c == "]": depth -= 1
        j += 1
    vd_close_bracket = j - 1
    vd_new_urls = ",".join(f'"{p["url"]}"' for p in PAGES)
    html = html[:vd_close_bracket] + "," + vd_new_urls + html[vd_close_bracket:]

    after = len(html.encode("utf-8"))
    with open(IDX, "w", encoding="utf-8") as f:
        f.write(html)
    print(f"Before size: {before} bytes")
    print(f"After size: {after} bytes")
    print(f"Delta: +{after-before} bytes")
    print(f"PROGRAMS appended: {len(PAGES)} entries")
    print(f"VD appended: {len(PAGES)} urls")
    print("Done!")

if __name__ == "__main__":
    main()
