# -*- coding: utf-8 -*-
"""Round 207: add 5 CS PhDs - USC / Yale / Princeton / Harvard SEAS / Vanderbilt."""
import os, re

BASE = os.path.dirname(os.path.abspath(__file__))
INDEX = os.path.join(BASE, "index_main.html")
DETAILS_DIR = os.path.join(BASE, "details")

new_programs = [
    {
        "id": "usc-cs-phd",
        "s": "usc",
        "n": "Computer Science (PhD)",
        "en": "PhD in Computer Science (University of Southern California, Viterbi School of Engineering, Department of Computer Science)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus research PhD, Los Angeles CA; Fall admission only (no Spring); applicant must hold or be completing a 4-year bachelor's degree in engineering, math, or a hard science; typically ~4 years fully funded; 13 Viterbi PhD programs total; applicants encouraged to contact faculty prior to applying.",
        "ddl": "Fall 2027: December 15, 2026, 11:59 PM Pacific Time (no Spring PhD admission; application must be submitted by deadline even if letters/scores not yet arrived).",
        "lang": "TOEFL or IELTS required for international applicants; waiver for applicants who earned a prior degree from USC.",
        "test": "GRE General: NOT REQUIRED for 2027 Viterbi PhD applications (all Viterbi PhD programs except Astronautical Engineering and Industrial & Systems Engineering; CS is exempt). Required: online application; official academic transcripts; personal statement; resume/CV; 3 letters of recommendation; evidence of research ability and experience. PhD students are typically fully funded: tuition + stipend for living expenses + health benefits package; ~4 years fully funded. Score distributions reflect admitted cohort, not minimum thresholds.",
        "tui": "Typically ~4 years fully funded: tuition + stipend + health benefits via Viterbi fellowship / RA / TA / external fellowship.",
        "url": "details/usc-cs-phd.html",
    },
    {
        "id": "yale-cs-phd",
        "s": "yale",
        "n": "Computer Science (PhD)",
        "en": "PhD in Computer Science (Yale University, Graduate School of Arts and Sciences, Department of Computer Science / School of Engineering and Applied Science)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus research PhD, New Haven CT; Fall admission only (no Spring; admitted students may defer with DGS approval); no part-time enrollment; research areas: algorithms, computational complexity, AI, networking, databases, graphics, ML, programming languages, robotics, scientific computing, security/privacy, systems.",
        "ddl": "December 15, 2026, 11:59 PM Eastern Time (Fall 2027 entry; MS deadline is January 2 separately).",
        "lang": "TOEFL iBT or IELTS Academic required for most applicants whose native language is not English; waiver if undergraduate degree is from an English-primary-instruction institution with at least 3 years of in-residence study.",
        "test": "GRE General: NOT ACCEPTED (not accepted for doctoral applicants; no penalty; scores will not be reviewed). Required: online GSAS application; transcripts; 3 letters; SOP; CV. PhD students at Yale are normally fully funded for 5 years: 12-month stipend covering living expenses + fellowship covering full tuition + student healthcare. Score distributions reflect admitted cohort, not minimum thresholds.",
        "tui": "5-year full funding: 12-month stipend + full tuition fellowship + student healthcare (GSAS PhD package).",
        "url": "details/yale-cs-phd.html",
    },
    {
        "id": "princeton-cs-phd",
        "s": "princeton",
        "n": "Computer Science (PhD)",
        "en": "PhD in Computer Science (Princeton University, Graduate School, Department of Computer Science)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus research PhD, Princeton NJ; Fall admission only (no Spring, evening, weekend, or distance programs); master's degree is not required for admission (students apply directly after bachelor's); ~5 years; admitted students may already hold a master's but that is not expected; strong CS/math/engineering background expected.",
        "ddl": "December 15, 11:59 PM Eastern Standard Time (Fall entry; application materials online from mid-September).",
        "lang": "TOEFL required for applicants whose native language is not English; MS program requires 28 speaking minimum; PhD follows Graduate School English screening.",
        "test": "GRE General: NOT REQUIRED and NOT ACCEPTED (scores will not be reviewed). Required: online Graduate School application; $75 application fee (waivers for US citizens/PRs with financial hardship, or participants in named pipeline programs); transcripts; 3 letters; SOP; CV. All admitted PhD students receive first-year Graduate School fellowship; years 2-5 via TA/RA; auto-considered for Wu and Upton Fellowships; external fellows (NSF, NDSEG, Hertz, DoD) welcome. Score distributions reflect admitted cohort, not minimum thresholds.",
        "tui": "All PhD students: tuition + health fee + monthly stipend for standard 5-year term; first-year Graduate School fellowship; auto-considered for Wu and Upton Fellowships.",
        "url": "details/princeton-cs-phd.html",
    },
    {
        "id": "harvard-cs-phd",
        "s": "harvard",
        "n": "Computer Science (PhD, SEAS)",
        "en": "PhD in Computer Science (Harvard University, John A. Paulson School of Engineering and Applied Sciences, conferred through Harvard Kenneth C. Griffin Graduate School of Arts and Sciences)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus research PhD, Cambridge MA; Fall admission only; no late applications; apply through GSAS, select 'Engineering and Applied Sciences' degree program and 'PhD Computer Science' in the Area of Study menu; complete the Supplemental SEAS Application Form; interdisciplinary ties: CRCS, IACS, Data Science Initiative, Berkman Klein Center.",
        "ddl": "December 15, 2026, 5:00 PM Eastern Time (PhD deadline; SM/ME/AB-SM master's deadline is December 1; no late applications; decisions mid-February; April 15 reply date).",
        "lang": "TOEFL iBT or IELTS Academic per Harvard Griffin GSAS policy for non-native speakers.",
        "test": "GRE General: NOT ACCEPTED (SEAS does not accept General GRE nor Subject GRE scores for PhD or CSE/DS master's; applicants must not submit official or unofficial GRE scores, nor mention them in application materials). Required: online GSAS application; Supplemental SEAS Application Form; transcripts; 3 letters; SOP; CV. Typical background: bachelor's in natural sciences, math, CS, or engineering. PhD students fully funded via GSAS package. Score distributions reflect admitted cohort, not minimum thresholds.",
        "tui": "PhD students fully funded via GSAS package: tuition + stipend + health fees (see GSAS PhD funding overview).",
        "url": "details/harvard-cs-phd.html",
    },
    {
        "id": "vanderbilt-cs-phd",
        "s": "vanderbilt",
        "n": "Computer Science (PhD)",
        "en": "PhD in Computer Science (Vanderbilt University, School of Engineering / College of Connected Computing, Department of Computer Science)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus research PhD, Nashville TN; Fall and Spring admission (Spring October 15); applications open August 1; first-year students typically serve as TA; funding continuation depends on academic/research progress; interdisciplinary with ECE (joint Engineering Graduate Fellowship).",
        "ddl": "Spring 2027: October 15, 2026; Fall 2027 priority: December 1, 2026 (fee waiver deadline); Fall 2027 final: December 15, 2026; decisions by March 31; reply by April 15.",
        "lang": "TOEFL iBT or IELTS Academic required for non-native speakers.",
        "test": "GRE General: EXEMPT / indefinitely waived (CS Graduate Program has indefinitely waived GRE scores). Required: online Vanderbilt Graduate School application; transcripts; 3 letters; SOP; CV. PhD students receive Engineering Graduate Fellowship: full tuition waiver + competitive monthly stipend + student service fees + health insurance premium; first-year TA; supplement with Provost's Graduate Fellowship ($10,000/yr up to 5 years), University Graduate Fellowship ($5,000/yr up to 5 years), or Dean's Graduate Fellowship ($6,000/yr). Score distributions reflect admitted cohort, not minimum thresholds.",
        "tui": "Full tuition waiver + competitive monthly stipend + health insurance + student service fees; supplement with Provost's / University / Dean's fellowships (up to $10k/yr).",
        "url": "details/vanderbilt-cs-phd.html",
    },
]

def detail_html(p, school_cn, tagline):
    stem_badge = "STEM" if p["stem"] else "Non-STEM"
    lvl_badge = "PhD" if p["lvl"] == "d" else "Master's"
    return f"""<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8">
<title>{p['en']} - {school_cn}</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.6}}
h1{{font-size:22px;border-bottom:2px solid #8c641a;padding-bottom:8px}}
h2{{font-size:16px;color:#8c641a;margin-top:24px}}
.grid{{display:grid;grid-template-columns:1fr 1fr;gap:12px}}
.card{{border:1px solid #ddd;border-radius:8px;padding:14px;background:#fafafa}}
.card h3{{margin:0 0 6px;font-size:14px;color:#444}}
.badge{{display:inline-block;background:#8c641a;color:#fff;padding:2px 8px;border-radius:10px;font-size:12px;margin-left:8px}}
.back{{display:inline-block;margin-bottom:16px;color:#8c641a;text-decoration:none}}
</style></head><body>
<a class="back" href="../index_main.html">&larr; Back to Program Library</a>
<h1>{p['en']}<span class="badge">{stem_badge}</span><span class="badge">{lvl_badge}</span></h1>
<p><strong>{school_cn}</strong> &middot; {p['deg']} &middot; {tagline}</p>
<div class="grid">
<div class="card"><h3>Degree / Format</h3>{p['deg']}, on-campus. {p['dur']}.</div>
<div class="card"><h3>Application Deadline</h3>{p['ddl']}.</div>
<div class="card"><h3>English Proficiency</h3>{p['lang']}.</div>
<div class="card"><h3>Standardized Tests</h3>{p['test']}.</div>
<div class="card"><h3>Tuition / Funding</h3>{p['tui']}.</div>
<div class="card"><h3>Curriculum</h3>待补 - qualifying exam, course distribution, thesis handbook to be filled from graduate program guide.</div>
</div>
<h2>Notes</h2>
<ul>
<li>Source: USC Viterbi / Yale GSAS / Princeton CS / Harvard SEAS GSAS / Vanderbilt Graduate School official admissions pages.</li>
<li>All five programs are research PhDs with full funding; none require GRE. Yale / Princeton / Harvard SEAS explicitly do not accept GRE (do not submit).</li>
<li>Princeton is the only one with a $75 application fee; Vanderbilt is the only one with Spring admission; USC Viterbi ~4 years fully funded.</li>
<li>Fields marked 待补 will be filled when the official graduate handbook PDF is reachable.</li>
<li>Official pages may update; always confirm on the school's admissions site before applying.</li>
</ul>
</body></html>"""

details_meta = [
    ("usc-cs-phd.html", "USC Viterbi School of Engineering", "PhD in Computer Science"),
    ("yale-cs-phd.html", "Yale University (GSAS / SEAS)", "PhD in Computer Science"),
    ("princeton-cs-phd.html", "Princeton University (Graduate School)", "PhD in Computer Science"),
    ("harvard-cs-phd.html", "Harvard SEAS (conferred via Griffin GSAS)", "PhD in Computer Science"),
    ("vanderbilt-cs-phd.html", "Vanderbilt University (School of Engineering)", "PhD in Computer Science"),
]

for fname, scn, tag in details_meta:
    p = next(p for p in new_programs if p["url"].endswith(fname))
    out = os.path.join(DETAILS_DIR, fname)
    with open(out, "w", encoding="utf-8") as f:
        f.write(detail_html(p, scn, tag))
    print("Wrote:", fname)

with open(INDEX, "r", encoding="utf-8") as f:
    html = f.read()

def fmt_prog(p):
    return (
        '{{id:"{id}",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}"}}'
    ).format(**p)

new_block = ",\n" + ",\n".join(fmt_prog(p) for p in new_programs)

m = re.search(r'const\s+PROGRAMS\s*=\s*\[', html)
if not m:
    raise SystemExit("PROGRAMS array not found")
start = m.end()
depth = 1
i = start
while i < len(html) and depth > 0:
    c = html[i]
    if c == '[':
        depth += 1
    elif c == ']':
        depth -= 1
    i += 1
insert_at = i - 1
new_html = html[:insert_at] + new_block + "\n" + html[insert_at:]

vd_new = [p["url"] for p in new_programs]
m2 = re.search(r'const\s+VERIFIED_DETAILS\s*=\s*new\s+Set\(\s*\[', new_html)
if not m2:
    raise SystemExit("VD Set not found")
start2 = m2.end()
depth2 = 1
j = start2
while j < len(new_html) and depth2 > 0:
    c = new_html[j]
    if c == '[':
        depth2 += 1
    elif c == ']':
        depth2 -= 1
    j += 1
insert_at2 = j - 1
vd_block = "," + ",".join('"{}"'.format(u) for u in vd_new)
new_html = new_html[:insert_at2] + vd_block + new_html[insert_at2:]

with open(INDEX, "w", encoding="utf-8") as f:
    f.write(new_html)

print("Appended {} PROGRAMS entries and {} VD entries".format(len(new_programs), len(vd_new)))
print("Done. File size:", os.path.getsize(INDEX))
