# -*- coding: utf-8 -*-
"""Round 204: add 5 CS PhDs - WashU CSE / Emory / Georgetown / Brown / Dartmouth."""
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": "wustl-cse-cpe-phd",
        "s": "wustl",
        "n": "Computer Engineering (PhD, CSE)",
        "en": "PhD in Computer Engineering (Washington University in St. Louis, McKelvey School of Engineering, Computer Science & Engineering Department)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus full-time research PhD, St. Louis MO; Fall entry only (Spring entry only if specifically invited by a faculty advisor); 72 units total: 33 graded course units including 9 breadth units, 3 seminar units, 24 research units; milestones: oral qualifying exam, dissertation proposal defense, dissertation defense; mentored teaching and Doctoral Student Research Seminar required.",
        "ddl": "December 15 (Fall entry; application opens September 1; decisions by March 15; reply by April 15)",
        "lang": "TOEFL iBT 90 minimum (institution code 6929); OR IELTS Academic 6.5; OR Duolingo 125; OR GATEWAY English 495; scores must be valid up to the application deadline.",
        "test": "GRE General: not listed on the official department checklist (use at your discretion; individual faculty may reference GRE in personal guidance). Required: online application; transcripts; 3 letters of recommendation; SOP; resume. All PhD students fully funded: full tuition remission + health insurance + living stipend + new high-end Apple laptop, guaranteed as long as satisfactory progress. Eligible for Olin-Chancellor's Fellowship and McDonnell International Scholars Academy. Score distributions reflect admitted cohort, not minimum thresholds.",
        "tui": "Fully funded: tuition + health insurance + stipend + Apple laptop (fellowship / RA / TA)",
        "url": "details/wustl-cse-cpe-phd.html",
    },
    {
        "id": "emory-cs-phd",
        "s": "emory",
        "n": "Computer Science and Informatics (PhD, CS & BMI tracks)",
        "en": "PhD in Computer Science and Informatics (Emory University, Department of Computer Science, Laney Graduate School; CS track and Biomedical Informatics / BMI track)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus full-time research PhD, Atlanta GA; two tracks: Computer Science, and Biomedical Informatics (joint with Emory School of Medicine); admits to PhD directly (master's not required); decisions sent between application deadline and April 15 per CGS resolution.",
        "ddl": "December 15 (Fall entry; application opens late summer; funding decisions follow CGS April 15 deadline)",
        "lang": "TOEFL iBT 85 minimum (90 recommended); paper-based TOEFL 600; OR IELTS Academic 7.0; Duolingo and PTE also accepted; waiver for applicants who earned a bachelor's or 2+ year master's in an English-primary-language country.",
        "test": "GRE General: NOT required but optional if submitted; preferred scores (if self-reporting) V150/Q165/AW3.5 - distribution, not minimum. Required: online application; transcripts (unofficial for application, official only upon offer); SOP; CV; 3 letters of recommendation (academic preferred); $75 application fee. PhD admissions typically fully funded through Laney Graduate School (tuition + stipend + insurance). Score distributions reflect admitted cohort, not minimum thresholds.",
        "tui": "Fully funded through Laney Graduate School (tuition + stipend + health insurance)",
        "url": "details/emory-cs-phd.html",
    },
    {
        "id": "geo-cs-phd",
        "s": "geo",
        "n": "Computer Science (PhD)",
        "en": "PhD in Computer Science (Georgetown University, College of Arts & Sciences, Department of Computer Science; STEM-designated)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus full-time research PhD, Washington DC; applicants may also apply simultaneously to the Interdisciplinary PhD in Cognitive Science program; admission and funding committed at the time of offer; full review of every submitted application.",
        "ddl": "Priority December 15; Final January 15 (Fall entry; application opens mid-July; decisions early March; applications after final deadline not reviewed)",
        "lang": "TOEFL iBT 90 minimum on the original scale (or 4.5 on the new iBT scale effective January 21, 2026); OR IELTS Academic 7.0 minimum; Graduate School floor is TOEFL 80 / IELTS 7.0, department requires the higher 90.",
        "test": "GRE General: NOT required (submission optional, absence not viewed negatively; no GRE Subject test). Required: B.S. or M.S. in CS / CE / IS / EE or closely related field; strong background in data structures, algorithms, hardware/architecture, programming, discrete math, probability/statistics, linear algebra, calculus; prior research experience or publication preferred; SOP; 3 letters; transcripts. PhD admits receive full financial aid package. Score distributions reflect admitted cohort, not minimum thresholds.",
        "tui": "Full financial aid committed at admission (tuition + stipend + insurance)",
        "url": "details/geo-cs-phd.html",
    },
    {
        "id": "brown-cs-phd",
        "s": "brown",
        "n": "Computer Science (PhD)",
        "en": "PhD in Computer Science (Brown University, Department of Computer Science, Graduate School)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus full-time research PhD, Providence RI; Fall entry only (September, no other start dates); 8 course credits over first two years (excluding Reading and Research); advancement to candidacy by end of year 2 (courses + programming requirement + research project); dissertation proposal within 2 years of candidacy; master's degree may be earned en route to PhD (terminal master's admits do not auto-transition to PhD).",
        "ddl": "December 15, 2026 (Fall 2027 entry; Fall-only admission; deadline includes test scores and recommendation letters)",
        "lang": "TOEFL iBT generally not below 105 (lower scores rarely admitted; PBT 620 / CBT 260); OR IELTS Academic accepted; waiver for applicants who received a college degree from an English-speaking institution.",
        "test": "GRE General: NOT mandatory (optional if submitted); GRE Subject: NOT required. Required: online application via Graduate School; unofficial transcripts; 3 letters of recommendation; SOP. All PhD students including international receive financial support; admission and funding are independent (do not need to mention aid in application). Score distributions reflect admitted cohort, not minimum thresholds.",
        "tui": "All PhD students (including international) fully funded",
        "url": "details/brown-cs-phd.html",
    },
    {
        "id": "dartmouth-cs-phd",
        "s": "dartmouth",
        "n": "Computer Science (PhD)",
        "en": "PhD in Computer Science (Dartmouth College, Department of Computer Science, Guarini School of Graduate and Advanced Studies)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus full-time research PhD, Hanover NH; admitted PhD students have the PhD as their sole degree objective (separate from the terminal MS track); guaranteed funding for up to five years; questions directed to phd@cs.dartmouth.edu.",
        "ddl": "Priority December 15, 2026; Final December 31, 2026 (Fall 2027 entry; apply via Guarini School, not Thayer School)",
        "lang": "TOEFL iBT / IELTS Academic / Duolingo accepted (ETS institution code 3351 for Guarini); waiver for applicants who have earned or are earning a bachelor's or higher degree from a U.S. institution or from a non-U.S. institution where English was the sole primary language of instruction.",
        "test": "GRE General: NOT accepted (do not send; scores will not be reviewed). Required: online application through Guarini School; transcripts (unofficial during application); SOP; CV; letters of recommendation; statement of purpose. Admitted doctoral students receive guaranteed funding up to 5 years (stipend + tuition + insurance). Score distributions reflect admitted cohort, not minimum thresholds.",
        "tui": "Guaranteed funding up to 5 years (stipend + tuition remission + insurance)",
        "url": "details/dartmouth-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: WashU McKelvey / Emory CS Laney / Georgetown CS / Brown CS / Dartmouth Guarini official admissions pages.</li>
<li>All five programs are research PhDs with full or near-full funding; none require GRE for the current cycle (Dartmouth does not accept GRE).</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 = [
    ("wustl-cse-cpe-phd.html", "Washington University in St. Louis (McKelvey Engineering)", "PhD in Computer Engineering, CSE Department"),
    ("emory-cs-phd.html", "Emory University (Laney Graduate School)", "PhD in Computer Science and Informatics"),
    ("geo-cs-phd.html", "Georgetown University (College of Arts & Sciences)", "PhD in Computer Science, STEM-designated"),
    ("brown-cs-phd.html", "Brown University (Department of Computer Science)", "PhD in Computer Science"),
    ("dartmouth-cs-phd.html", "Dartmouth College (Guarini School)", "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))
