# -*- coding: utf-8 -*-
"""Round 201: add 5 - MIT DEDP MASc / Yale ECE MS / UW ECE MASc / Harvard Business Econ PhD / UCSD CSE PhD Bioinfo."""
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-dedp-masc",
        "s": "mit",
        "n": "Data, Economics, and Design of Policy (MASc)",
        "en": "Master of Applied Science in Data, Economics, and Design of Policy (MIT Department of Economics x Abdul Latif Jameel Poverty Action Lab / J-PAL)",
        "deg": "MASc",
        "lvl": "m",
        "stem": 1,
        "dur": "On-campus, Cambridge MA; 1 semester of coursework at MIT plus a summer capstone (internship + project report); two tracks: International Development (low/middle-income contexts) and Public Policy (high-income contexts). ADMISSION PATHWAY: applicants must first earn the MITx DEDP MicroMasters credential (5 courses in the chosen track).",
        "ddl": "Fall only; MicroMasters exam cycle fall -> application deadline typically mid-December (see economics.mit.edu/masters for the exact cycle)",
        "lang": "TOEFL iBT 5/6 (new 6-point scale since 2026-01-17; prior requirement 100/120); IELTS Academic 7; Duolingo English Test 135. Home edition TOEFL iBT accepted; TOEFL ITP not accepted.",
        "test": "GRE: NOT accepted (per Economics FAQ); GMAT not accepted. No prior bachelor's/master's degree required but transcripts for any university-level work are required. Required: MITx DEDP MicroMasters credential in corresponding track; 2 letters of recommendation; statement of objectives (<=2 pages double-spaced); research proposal (<=2 pages); resume/CV; short video recording; $75 application fee. Score distributions (when published) reflect admitted cohort, not minimum thresholds.",
        "tui": "待补 (see MIT Economics DEDP tuition; one-semester on-campus residency plus summer capstone)",
        "url": "details/mit-dedp-masc.html",
    },
    {
        "id": "yale-ece-ms",
        "s": "yale",
        "n": "Electrical and Computer Engineering (MS)",
        "en": "Master of Science in Electrical and Computer Engineering (Yale School of Engineering & Applied Science, ECE Department)",
        "deg": "MS",
        "lvl": "m",
        "stem": 1,
        "dur": "On-campus terminal MS, New Haven CT; coursework-based (the Graduate Student Handbook lists required and elective graduate courses); DGS Hong Tang; fields include biomedical sensory systems, communications/signal processing, neural networks, control, wireless/sensor networks, MEMS/NEMS, nanoelectronic science, optoelectronics, semiconductor materials, photonics, quantum materials, computer engineering, computer architecture, hardware security, neuromorphic computing, VLSI.",
        "ddl": "December 15, 11:59 PM ET (Fall admission)",
        "lang": "TOEFL iBT or IELTS Academic required of most applicants whose native language is not English (Yale GSAS rule).",
        "test": "GRE: Optional (submission is discretionary). Master's programs at Yale are generally NOT funded (PhD students receive 12-month stipend + tuition fellowship + health fees). Required: bachelor's degree in engineering/related quantitative field; statement of purpose; transcripts; 3 letters of recommendation; resume. Score distributions (when published) reflect admitted cohort, not minimum thresholds.",
        "tui": "待补 (see Yale GSAS tuition; MS generally unfunded)",
        "url": "details/yale-ece-ms.html",
    },
    {
        "id": "uw-ece-masc",
        "s": "uw",
        "n": "Electrical and Computer Engineering (MASc)",
        "en": "Master of Applied Science in Electrical and Computer Engineering (University of Waterloo, Department of Electrical and Computer Engineering)",
        "deg": "MASc",
        "lvl": "m",
        "stem": 1,
        "dur": "On-campus research-based thesis master, Waterloo ON Canada; 24 months (6 terms) full-time; 5 courses + original research + thesis; 14 specialization areas (incl. AI/ML, biomedical engineering, computer networking & security, nanoelectronic circuits/devices, software, sustainable energy); collaborative programs in Nanotechnology or Quantum Information. Co-op not available for MASc (co-op is only through MEng).",
        "ddl": "Rolling by term: February 1 (Fall/September entry), June 1 (Winter/January entry), October 1 (Spring/May entry).",
        "lang": "TOEFL iBT 90 with writing 21 / speaking 22 / reading 22 / listening 22 (note: UW ECE page cites older scale 4.5 with sub-scores); IELTS Academic 6.5 overall with writing 6.0 / speaking 6.0 / reading 6.0 / listening 6.0.",
        "test": "GPA: 75% overall standing in last two years of a relevant 4-year Honours bachelor's (Canadian or equivalent international). Required: faculty supervisor endorsement at time of admission (not required at application; applicants should identify a potential supervisor before applying); 2 references (at least one academic); resume; statement of interest (program-specific questions); transcripts. Score distributions (when published) reflect admitted cohort, not minimum thresholds.",
        "tui": "待补 (see UW Graduate Studies tuition; research MASc typically funded by supervisor RA/TA)",
        "url": "details/uw-ece-masc.html",
    },
    {
        "id": "harvard-bus-eco-phd",
        "s": "harvard",
        "n": "Business Economics (PhD)",
        "en": "PhD in Business Economics (Harvard Griffin GSAS x Harvard Business School, interfaculty doctoral program)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus full-time research PhD, Cambridge MA; jointly enrolled in Harvard Griffin GSAS and HBS; leads toward academic/research careers in economics, finance, accounting, management, marketing. JD/PhD coordinated track available (separate HLS application).",
        "ddl": "December 15, 5:00 PM ET (Fall admission; HBS Health Policy uses Dec 1; Business Economics uses Dec 15).",
        "lang": "TOEFL iBT preferred minimum 100; IELTS Academic preferred minimum 7. Not required for graduates of 4-year English-speaking institutions.",
        "test": "GRE General or GMAT: REQUIRED. Required: bachelor's degree (senior-year applicants welcome); statement of purpose; resume/CV; transcripts; 3 letters of recommendation; writing sample optional but highly recommended (<=10 pages of text plus references). PhD students receive full funding (stipend + tuition + health fees) per HBS doctoral support. Score distributions (when published) reflect admitted cohort, not minimum thresholds.",
        "tui": "Fully funded per HBS doctoral program support (stipend + tuition fellowship + health insurance)",
        "url": "details/harvard-bus-eco-phd.html",
    },
    {
        "id": "ucsd-cse-bioinfo-phd",
        "s": "ucsd",
        "n": "Computer Science (PhD, Bioinformatics Specialization)",
        "en": "PhD in Computer Science with a specialization in Bioinformatics (UC San Diego Department of Computer Science and Engineering)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 1,
        "dur": "On-campus full-time research PhD, La Jolla CA; CSE PhD with bioinformatics as one of 13 listed research areas (algorithms, AI, bioinformatics, architecture/compilers, graphics/vision, databases, embedded systems, HPC, HCI, programming systems, security, SE, systems/networking, ubiquitous computing, VLSI/CAD). Fall quarter admission only; separate from the cross-departmental Bioinformatics & Systems Biology PhD (major code BF76).",
        "ddl": "December 17, 11:59 PM PST (Fall quarter admission only; applications open September 3).",
        "lang": "TOEFL iBT (Home Edition accepted); TOEFL ITP Plus for China NOT accepted; IELTS accepted. UCSD institution code 4836 if self-reporting GRE (GRE not required).",
        "test": "GRE: NOT required for Fall 2026/2027 (may submit if already taken). Required: SOP; transcripts; 3 letters of recommendation; resume. Quantitative/computational background preferred. PhD students funded via fellowships / research assistantships / teaching assistantships. Score distributions (when published) reflect admitted cohort, not minimum thresholds.",
        "tui": "Fully funded via fellowship / RA / TA (stipend + tuition + health insurance)",
        "url": "details/ucsd-cse-bioinfo-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>待补 - course list, credits, concentrations to be filled from official bulletin / graduate handbook.</div>
</div>
<h2>Notes</h2>
<ul>
<li>Source: MIT Economics / Yale SEAS / UWaterloo ECE / Harvard Griffin GSAS & HBS / UCSD CSE official admissions pages.</li>
<li>MIT DEDP requires the MITx MicroMasters credential as a pre-application condition; it is not a standard direct-entry master.</li>
<li>Fields marked 待补 will be filled when the official bulletin / 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-dedp-masc.html", "MIT (Department of Economics x J-PAL)", "Master of Applied Science in Data, Economics, and Design of Policy"),
    ("yale-ece-ms.html", "Yale University (SEAS)", "Electrical and Computer Engineering, terminal MS"),
    ("uw-ece-masc.html", "University of Waterloo (Faculty of Engineering)", "Master of Applied Science in ECE, research thesis"),
    ("harvard-bus-eco-phd.html", "Harvard University (GSAS x HBS)", "PhD in Business Economics, interfaculty"),
    ("ucsd-cse-bioinfo-phd.html", "UC San Diego (Jacobs School / CSE)", "PhD in Computer Science, Bioinformatics specialization"),
]

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