# -*- coding: utf-8 -*-
"""Round 196: add 5 programs - UCLA Stats MS / NYU Africana MA / McGill MISt / UCSD BioE MS Med / NYU Classics MA."""
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": "ucla-stats-ms",
        "s": "ucla",
        "n": "Statistics (MS)",
        "en": "Master of Science in Statistics (Department of Statistics and Data Science)",
        "deg": "MS",
        "lvl": "m",
        "stem": 1,
        "dur": "On-campus, Fall only, Los Angeles CA",
        "ddl": "MS: Jan 15, 2027 (Fall 2027); PhD: Dec 10, 2026",
        "lang": "TOEFL iBT min (W25/S24/R21/L17); IELTS accepted; UCLA code 4837",
        "test": "GRE highly recommended (not required); 3 letters; upper-division GPA 3.2+; ~12 quarter quantitative courses",
        "tui": "待补 (see UCLA graduate tuition)",
        "url": "details/ucla-stats-ms.html",
    },
    {
        "id": "nyu-africana-ma",
        "s": "nyu",
        "n": "Africana Studies (MA)",
        "en": "Master of Arts in Africana Studies (GSAS Department of Social and Cultural Analysis)",
        "deg": "MA",
        "lvl": "m",
        "stem": 0,
        "dur": "On-campus, Fall admission, New York NY",
        "ddl": "Jan 4 (with funding consideration); Mar 15 (final, no funding)",
        "lang": "TOEFL / IELTS (GSAS code 2596); GSAS recommends TOEFL 100 / IELTS 7",
        "test": "GRE not required - do not send; 3 letters; statement of purpose",
        "tui": "待补 (see GSAS tuition)",
        "url": "details/nyu-africana-ma.html",
    },
    {
        "id": "mcg-mist",
        "s": "mcg",
        "n": "Information Studies (MISt)",
        "en": "Master of Information Studies (School of Information Studies, ALA-accredited)",
        "deg": "MISt",
        "lvl": "m",
        "stem": 0,
        "dur": "48 credits, ~2 years campus-based, Montreal QC; Fall (September) intake",
        "ddl": "International / Canadian: Jan 15 (Fall admission)",
        "lang": "TOEFL iBT 100 (writing 25, reading/speaking/listening 20+); IELTS 7.5 (writing 7.5)",
        "test": "No GRE required; CGPA 3.0/4.0 minimum; concentrations incl. Archival Studies / IT / Knowledge Management / Library Studies / UX / Data Curation",
        "tui": "待补 (see McGill graduate tuition)",
        "url": "details/mcg-mist.html",
    },
    {
        "id": "ucsd-bioe-ms-med",
        "s": "ucsd",
        "n": "Bioengineering with Medical Specialization (MS)",
        "en": "Master of Science in Bioengineering with a Medical Specialization (MS Med)",
        "deg": "MS",
        "lvl": "m",
        "stem": 1,
        "dur": "48 units, 3-4 quarters full-time, La Jolla CA; Fall quarter only",
        "ddl": "Fall quarter only (see BE graduate admissions page for exact deadline)",
        "lang": "TOEFL iBT 85 min, speaking 23 min; IELTS band 7",
        "test": "GPA 3.4+ in technical courses; 3 letters; engineering/natural sciences/math/quantitative life sciences background",
        "tui": "待补 (see UCSD graduate tuition)",
        "url": "details/ucsd-bioe-ms-med.html",
    },
    {
        "id": "nyu-classics-ma",
        "s": "nyu",
        "n": "Classics (MA)",
        "en": "Master of Arts in Classics (GSAS Department of Classics)",
        "deg": "MA",
        "lvl": "m",
        "stem": 0,
        "dur": "On-campus, Fall admission, New York NY",
        "ddl": "Jan 4 (Fall admission, 5PM ET)",
        "lang": "TOEFL / IELTS (GSAS code 2596); recommends TOEFL 100 / IELTS 7",
        "test": "GRE not required - do not send; 3 letters; writing sample (up to 35 double-spaced pages); Greek/Latin undergraduate background required",
        "tui": "待补 (see GSAS tuition)",
        "url": "details/nyu-classics-ma.html",
    },
]

def detail_html(p, school_cn, tagline):
    stem_badge = "STEM" if p["stem"] else "Non-STEM"
    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 #1a5fb4;padding-bottom:8px}}
h2{{font-size:16px;color:#1a5fb4;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:#1a5fb4;color:#fff;padding:2px 8px;border-radius:10px;font-size:12px;margin-left:8px}}
.back{{display:inline-block;margin-bottom:16px;color:#1a5fb4;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></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']}. Score distributions (when published) reflect admitted cohort, not minimum thresholds.</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.</div>
</div>
<h2>Notes</h2>
<ul>
<li>Source: official university / school admissions page.</li>
<li>Fields marked 待补 will be filled when the official bulletin / bulletin 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 = [
    ("ucla-stats-ms.html", "UCLA", "Department of Statistics and Data Science"),
    ("nyu-africana-ma.html", "NYU", "GSAS Department of Social and Cultural Analysis"),
    ("mcg-mist.html", "McGill University", "School of Information Studies (ALA-accredited)"),
    ("ucsd-bioe-ms-med.html", "UC San Diego", "Shu Chien - Gene Lay Department of Bioengineering"),
    ("nyu-classics-ma.html", "NYU", "GSAS Department of Classics"),
]

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