# -*- coding: utf-8 -*-
"""Round 203: add 5 PhDs - MIT EECS / Cornell ECE / UIUC ECE / GaTech ECE / UMich ECE."""
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": "mit-eecs-phd",
        "s": "mit",
        "n": "Electrical Engineering and Computer Science (PhD)",
        "en": "PhD in Electrical Engineering and Computer Science (MIT Department of Electrical Engineering and Computer Science, EECS)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus full-time research PhD, Cambridge MA; ~700 doctoral students in the program; no terminal master - everyone applies directly to PhD; students earn an SM (Master of Science) en route; MEng only available to MIT EECS undergrads. Application opens September 15.",
        "ddl": "December 1, 2026 (Fall 2027 entry only; decisions posted via MIT Application Management system)",
        "lang": "TOEFL iBT 100 (institution code 3514, dept code 78 or 66) OR IELTS Academic 7 (IELTS preferred over TOEFL); waivers available for English-medium backgrounds.",
        "test": "GRE General: NOT required. Required: online application; 2 essays (Essay 1 research activities and goals; Essay 2 academic and intellectual journey); 3 letters of recommendation; transcripts; strong math/physics/engineering/CS background. Fully funded throughout: fellowship/RA/TA covering full tuition + monthly living stipend + medical insurance. Score distributions (when published) reflect admitted cohort, not minimum thresholds.",
        "tui": "Fully funded: tuition + monthly stipend + medical insurance (fellowship / RA / TA)",
        "url": "details/mit-eecs-phd.html",
    },
    {
        "id": "cornell-ece-phd",
        "s": "cornell",
        "n": "Electrical and Computer Engineering (PhD)",
        "en": "PhD in Electrical and Computer Engineering (Cornell University, Duffield College of Engineering, School of ECE, Ithaca)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus full-time research PhD, Ithaca NY; admitted directly to PhD (master's not required); before second year must pass qualifying examination (subject-area exams by ECE faculty); interdisciplinary research spanning power/energy, bioelectrical engineering, computer architecture, imaging, nanotechnology, photonics, neuroscience, computing.",
        "ddl": "December 15 (Fall admission only; hard deadline, no Spring admission; no exceptions)",
        "lang": "TOEFL iBT minimum: listening 15, writing 20, reading 20, speaking 22; OR IELTS overall 7.0; scores valid only for the admissions cycle applied.",
        "test": "GRE General: NOT required (if submitted, will not be reviewed). Required: GPA 3.5+; 3 letters of recommendation (at least 2 from faculty); unofficial transcripts; English proficiency; SOP. Virtually all PhD students receive full financial support: fellowship + GRA + GTA covering tuition + stipend + health insurance. Score distributions (when published) reflect admitted cohort, not minimum thresholds.",
        "tui": "Fully funded: tuition + stipend + health insurance (fellowship / GRA / GTA)",
        "url": "details/cornell-ece-phd.html",
    },
    {
        "id": "illinois-ece-phd",
        "s": "illinois",
        "n": "Electrical and Computer Engineering (PhD)",
        "en": "PhD in Electrical and Computer Engineering (University of Illinois Urbana-Champaign, Grainger College of Engineering, Department of ECE)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus full-time research PhD, Urbana-Champaign IL; admits Fall and Spring; minimum GPA 3.0 (last two undergrad years), but GPAs below 3.5 rarely admitted; Grainger Engineering PhD students in first 5 years guaranteed funded appointment for Fall/Spring with full tuition waiver + partial fee waiver + stipend.",
        "ddl": "Fall: January 15 (fellowship priority deadline December 15); Spring: September 1; decisions typically by April 2 for summer/fall",
        "lang": "TOEFL iBT 96 minimum; OR IELTS Academic 6.5 overall with 6.0 in all subsections; waiver for English-medium undergrad institutions in US/Canada.",
        "test": "GRE General: OPTIONAL / no longer required for graduate applications. Required: bachelor's in EE/CE or related (physical sciences/math/CS considered if background sufficient); 3 letters of reference; SOP; ECE Graduate Applicant Preferences Form (research area of interest). 100% of PhD students receive funding. Score distributions (when published) reflect admitted cohort, not minimum thresholds.",
        "tui": "Guaranteed funding for first 5 years: full tuition waiver + partial fee waiver + stipend (fellowship / RA / TA)",
        "url": "details/illinois-ece-phd.html",
    },
    {
        "id": "gatech-ece-phd",
        "s": "gatech",
        "n": "Electrical and Computer Engineering (PhD)",
        "en": "PhD in Electrical and Computer Engineering (Georgia Institute of Technology, School of Electrical and Computer Engineering, Atlanta campus)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus full-time research PhD, Atlanta GA; research-oriented and highly individualized; typically at least 4 years beyond bachelor's; two semesters full-time on-campus residency; minimum 9 hours letter-graded at Georgia Tech; maintain 3.0 GPA; after candidacy, all degree requirements within 7 years. Coursework Qualifier: 3.5 GPA in 4 courses selected by ECE Technical Interest Groups.",
        "ddl": "Atlanta campus MS and PhD Fall: December 16 (GT Europe / Shenzhen / Distance learning are separate master's-only tracks, not PhD)",
        "lang": "TOEFL iBT 90 minimum (pre-2026: 19 per section; post-2026: new scale 4.5 overall with R4/L4/W4/S3.5); institution code 5248; exam results 2-year validity.",
        "test": "GRE General: OPTIONAL (not required; if submitted, will be considered and strong scores may offset weaknesses; not a primary factor). Required: SOP; 3 letters of recommendation (online only); transcripts; $85 application fee. Most Atlanta PhD admits receive GTA or GRA offer noted in acceptance letter. Score distributions (when published) reflect admitted cohort, not minimum thresholds.",
        "tui": "Most PhD admits receive GTA/GRA covering tuition + stipend; external fellowships encouraged",
        "url": "details/gatech-ece-phd.html",
    },
    {
        "id": "umich-ece-phd",
        "s": "umich",
        "n": "Electrical and Computer Engineering (PhD)",
        "en": "PhD in Electrical and Computer Engineering (University of Michigan, College of Engineering, Department of ECE, Rackham Graduate School)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus full-time research PhD, Ann Arbor MI; Fall admission only (no Winter for external PhD); CSE PhD separate but same Rackham framework; admitted PhD profile 2025-26: average GPA 3.7; research experience especially important for PhD applicants.",
        "ddl": "Fall 2027: December 15, 2026 (no Winter admission for external PhD); application opens October 1, 2026",
        "lang": "TOEFL iBT 84 minimum; OR IELTS Academic 6.5; OR MELAB 80; waiver for English-medium bachelor's/master's institution.",
        "test": "GRE General: NOT included in PhD admissions process (as of Fall 2027 cycle). Required: bachelor's degree (master's not required); Academic Statement of Purpose 1-3 pages; Personal Statement 1 page; 3 letters of recommendation (at least 2 from faculty/instructors). All PhD applicants automatically considered for departmental financial aid (no separate application). Score distributions (when published) reflect admitted cohort, not minimum thresholds.",
        "tui": "Automatically considered for departmental financial aid (fellowship / RA / TA) covering tuition + stipend",
        "url": "details/umich-ece-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, coursework, thesis requirements to be filled from graduate handbook.</div>
</div>
<h2>Notes</h2>
<ul>
<li>Source: MIT EECS / Cornell ECE / UIUC ECE / Georgia Tech ECE / UMich ECE official admissions pages.</li>
<li>All five programs are research PhDs with full or near-full funding; none require GRE for the current cycle.</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 = [
    ("mit-eecs-phd.html", "MIT (EECS)", "PhD in Electrical Engineering and Computer Science"),
    ("cornell-ece-phd.html", "Cornell University (Duffield Engineering)", "PhD in Electrical and Computer Engineering, Ithaca"),
    ("illinois-ece-phd.html", "University of Illinois Urbana-Champaign (Grainger Engineering)", "PhD in Electrical and Computer Engineering"),
    ("gatech-ece-phd.html", "Georgia Tech (Atlanta campus)", "PhD in Electrical and Computer Engineering"),
    ("umich-ece-phd.html", "University of Michigan (Rackham Graduate School)", "PhD in Electrical and Computer Engineering"),
]

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))
