# -*- coding: utf-8 -*-
"""Round 197: add 5 humanities MA programs - UChicago MAPH / Columbia French MA / Columbia Italian MA / NYU German MA / UCLA Slavic 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": "uchicago-maph",
        "s": "uchicago",
        "n": "Master of Arts Program in the Humanities (MAPH)",
        "en": "Master of Arts Program in the Humanities (Division of the Humanities)",
        "deg": "MA",
        "lvl": "m",
        "stem": 0,
        "dur": "1-year full-time on-campus (also part-time option available); Autumn quarter start; Chicago IL",
        "ddl": "2027-28 rounds: R1 Nov 2, 2026; R2 Feb 1, 2027; R3 May 3, 2027; BA/MA Feb 2, 2027",
        "lang": "TOEFL / IELTS required (UChicago Graduate Division English proficiency rules apply)",
        "test": "GRE not required and not accepted; $90 application fee; cohort 100-150; MA thesis supervised by UChicago faculty; concentrations incl. Art History / English / Philosophy / Music / Romance Languages / Race Diaspora & Indigeneity / interdisciplinary",
        "tui": "待补 (see UChicago Graduate Division tuition)",
        "url": "details/uchicago-maph.html",
    },
    {
        "id": "col-french-ma",
        "s": "col",
        "n": "French (MA, free-standing)",
        "en": "Master of Arts in French (GSAS Department of French and Romance Philology, free-standing)",
        "deg": "MA",
        "lvl": "m",
        "stem": 0,
        "dur": "Full-time or part-time; 9 courses over ~2 years; MA essay; New York NY",
        "ddl": "Fall admission: Apr 8, 2027; Spring admission: Oct 29, 2026",
        "lang": "TOEFL / IELTS required for non-native English speakers (GSAS code 2163)",
        "test": "GRE General: No; 3 letters (at least 2 academic); 2 writing samples (one in English, one in French, 10-15 pp double-spaced); CV; high proficiency in French and English; MA essay required",
        "tui": "待补 (see GSAS tuition; MA self-funded, PhD fully funded)",
        "url": "details/col-french-ma.html",
    },
    {
        "id": "col-italian-ma",
        "s": "col",
        "n": "Italian (MA, free-standing)",
        "en": "Master of Arts in Italian (GSAS Department of Italian, free-standing)",
        "deg": "MA",
        "lvl": "m",
        "stem": 0,
        "dur": "1 academic year full-time; 10 one-term courses (2 Residence Units); New York NY",
        "ddl": "Fall admission: Jan 7, 2027; Spring admission: Oct 29, 2026",
        "lang": "TOEFL / IELTS required for non-native English speakers (GSAS code 2163)",
        "test": "GRE General: No; 3 letters (at least 2 academic); writing sample 10-15 pp double-spaced; CV; reading ability in a relevant foreign language required; 8 graduate literature/culture courses (3 pre-1600, 3 post-1600) + 2 R-credit courses",
        "tui": "待补 (see GSAS tuition; MA self-funded, PhD fully funded)",
        "url": "details/col-italian-ma.html",
    },
    {
        "id": "nyu-german-ma",
        "s": "nyu",
        "n": "German Thought and Literature (MA)",
        "en": "Master of Arts in German Thought and Literature / Germanic Languages and Literatures (GSAS Department of German)",
        "deg": "MA",
        "lvl": "m",
        "stem": 0,
        "dur": "On-campus, Fall admission only, New York NY",
        "ddl": "MA: April 15 (Fall admission, 5PM ET); PhD final: Jan 4",
        "lang": "TOEFL / IELTS (GSAS code 2596); recommends TOEFL 100 / IELTS 7",
        "test": "GRE not required - do not send; 3 letters; personal statement; writing sample required; German language proficiency required; German literature / culture / thought background",
        "tui": "待补 (see GSAS tuition)",
        "url": "details/nyu-german-ma.html",
    },
    {
        "id": "ucla-slavic-ma",
        "s": "ucla",
        "n": "Slavic, East European and Eurasian Languages and Cultures (MA)",
        "en": "Master of Arts in Slavic, East European, and Eurasian Languages and Cultures (Department of Slavic, East European & Eurasian Languages & Cultures)",
        "deg": "MA",
        "lvl": "m",
        "stem": 0,
        "dur": "On-campus, Fall quarter only, Los Angeles CA; MA on path to PhD",
        "ddl": "Dec 15, 2026 (Fall 2027 admission); decisions Feb-Mar 2027",
        "lang": "TOEFL iBT min 87 (UCLA Graduate Division minimum); IELTS accepted; UCLA code 4837",
        "test": "GRE: Not required (2026-2027); 3 letters; statement of purpose; writing sample in field of interest; undergraduate equivalent in Russian/Slavic/Russian Studies, or 3 years of Russian + courses in Russian history/literature/linguistics; two foreign languages required for MA",
        "tui": "待补 (see UCLA graduate tuition; PhD-track funding packages considered)",
        "url": "details/ucla-slavic-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 / GSAS / Graduate Division 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 = [
    ("uchicago-maph.html", "University of Chicago", "Division of the Humanities"),
    ("col-french-ma.html", "Columbia University", "GSAS Department of French and Romance Philology"),
    ("col-italian-ma.html", "Columbia University", "GSAS Department of Italian"),
    ("nyu-german-ma.html", "NYU", "GSAS Department of German"),
    ("ucla-slavic-ma.html", "UCLA", "Department of Slavic, East European & Eurasian Languages & Cultures"),
]

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