# -*- coding: utf-8 -*-
"""Round 306: UCSD 2 PhD (Physical Oceanography / Engineering Sciences Joint SDSU) + USC 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": "ucsd-phys-ocean-phd",
        "s": "ucsd",
        "n": "物理海洋学博士",
        "en": "Physical Oceanography PhD",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-01-05",
        "lang": "TOEFL 100+/IELTS 7.0+",
        "test_cls": "GRE General 不要求（SIO 政策）",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $36,000/年，2025-26）+ 医保",
        "url": "details/ucsd-phys-ocean-phd.html",
        "school": "UC San Diego",
        "dept": "Scripps Institution of Oceanography",
        "college": "Scripps Institution of Oceanography",
        "website": "https://scripps.ucsd.edu/academics/phd-programs/physical-oceanography",
        "overview": "加州大学圣地亚哥分校物理海洋学博士项目（Physical Oceanography PhD）是 Scripps 海洋研究所核心 PhD，研究海洋环流、 Waves、海气相互作用、海洋气候与海洋建模。",
        "requirements": "要求物理、数学、工程或海洋相关本科背景；流体力学、物理、微积分、微分方程先修；3 封推荐信；SOP；CV；Research Experience。",
        "research": "研究方向包括：海洋环流；海浪与湍流；海气相互作用；海洋气候；海洋建模；卫星海洋学；极地海洋学；海洋声学；海洋数据同化；厄尔尼诺与气候预测。",
    },
    {
        "id": "ucsd-eng-sci-phd",
        "s": "ucsd",
        "n": "工程科学博士（与SDSU联合）",
        "en": "Engineering Sciences PhD (Joint with SDSU)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-12-15",
        "lang": "TOEFL 100+/IELTS 7.0+",
        "test_cls": "GRE General 不要求（UCSD Jacobs 政策）",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $36,000/年，2025-26）+ 医保",
        "url": "details/ucsd-eng-sci-phd.html",
        "school": "UC San Diego",
        "dept": "Jacobs School of Engineering / SDSU Joint Doctoral Program",
        "college": "Jacobs School of Engineering",
        "website": "https://jacobsschool.ucsd.edu/graduate/programs/engineering-sciences-joint-sdsu",
        "overview": "加州大学圣地亚哥分校工程科学博士项目（Engineering Sciences PhD, Joint with SDSU）是 UCSD 与 San Diego State University 联合博士项目，学生跨两校选课与导师。",
        "requirements": "要求工程、物理、数学或相关本科背景；工程核心课程先修；3 封推荐信；SOP；CV；Research Experience。",
        "research": "研究方向包括：航空航天工程；生物工程；化学工程；土木工程；计算机工程；电机工程；机械工程；结构工程；应用力学；材料科学。",
    },
    {
        "id": "usc-marine-eb-phd",
        "s": "usc",
        "n": "海洋环境生物学博士",
        "en": "Marine Environmental Biology PhD",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-12-15",
        "lang": "TOEFL 90+/IELTS 6.5+",
        "test_cls": "GRE General 不要求（USC Dornsife 政策）",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $34,000/年，2025-26）+ 医保",
        "url": "details/usc-marine-eb-phd.html",
        "school": "University of Southern California",
        "dept": "Department of Biological Sciences",
        "college": "Dornsife College of Letters, Arts and Sciences",
        "website": "https://dornsife.usc.edu/labs/marine-biology/",
        "overview": "南加州大学海洋环境生物学博士项目（Marine Environmental Biology PhD）研究海洋生物、海洋生态、海洋保护与全球变化对海洋生态系统的影响。",
        "requirements": "要求生物、生态、海洋或相关本科背景；生物学、化学、生态学先修；3 封推荐信；SOP；CV；Research Experience。",
        "research": "研究方向包括：海洋生态；海洋保护；海洋生物多样性；气候变化与海洋；海洋污染；海洋微生物生态；珊瑚礁生态；渔业生态学；海洋生物地球化学；海洋遥感。",
    },
    {
        "id": "usc-ppss-phd",
        "s": "usc",
        "n": "人口、公共与社会科学博士",
        "en": "Population, Public, and Social Sciences PhD",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-12-15",
        "lang": "TOEFL 90+/IELTS 6.5+",
        "test_cls": "GRE General 不要求（USC Dornsife 政策）",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $34,000/年，2025-26）+ 医保",
        "url": "details/usc-ppss-phd.html",
        "school": "University of Southern California",
        "dept": "Department of Sociology / Price School of Public Policy",
        "college": "Dornsife College / Price School",
        "website": "https://dornsife.usc.edu/sociology/ppss/",
        "overview": "南加州大学人口、公共与社会科学博士项目（Population, Public, and Social Sciences PhD, PPSS）是跨系博士项目，研究人口、公共政策与社会不平等。",
        "requirements": "要求社会科学、公共政策或相关本科背景；统计学、社会研究方法先修；3 封推荐信；SOP；CV；Research Experience。",
        "research": "研究方向包括：人口学；移民研究；健康不平等；城市社会学；教育政策；社会分层；家庭与人口；老龄化；社会政策；计算社会科学。",
    },
    {
        "id": "usc-earth-sci-phd",
        "s": "usc",
        "n": "地球科学博士",
        "en": "Earth Sciences PhD",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "5-6 年",
        "ddl": "2026-12-15",
        "lang": "TOEFL 90+/IELTS 6.5+",
        "test_cls": "GRE General 不要求（USC Dornsife 政策）",
        "tui": "PhD 全额资助：学费全免 + stipend（约 $34,000/年，2025-26）+ 医保",
        "url": "details/usc-earth-sci-phd.html",
        "school": "University of Southern California",
        "dept": "Department of Earth Sciences",
        "college": "Dornsife College of Letters, Arts and Sciences",
        "website": "https://dornsife.usc.edu/earth/",
        "overview": "南加州大学地球科学博士项目（Earth Sciences PhD）研究地球系统、气候、地质、地球物理与地球化学。",
        "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()
