# -*- coding: utf-8 -*-
"""Round 198: add 5 humanities MA - Columbia Germanic / Columbia MESAAS / NYU Cinema Studies / NYU Hebrew & Judaic / Columbia Religion."""
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": "col-german-ma",
        "s": "col",
        "n": "Germanic Languages (MA, free-standing)",
        "en": "Master of Arts in Germanic Languages (GSAS Department of Germanic Languages, free-standing)",
        "deg": "MA",
        "lvl": "m",
        "stem": 0,
        "dur": "Full-time; 10 courses (7 in Dept of Germanic Languages; 6 letter-grade + 4 R credit); MA Exam; New York NY",
        "ddl": "Fall admission: Mar 4, 2027; Spring admission: Oct 29, 2026; PhD final: Jan 7, 2027",
        "lang": "TOEFL / IELTS required for non-native English speakers (GSAS code 2163)",
        "test": "GRE General: No (optional per department); 3 academic letters; personal statement; writing sample 15-20 pp double-spaced, preferably in German; exceptional German fluency required",
        "tui": "待补 (see GSAS tuition; MA self-funded)",
        "url": "details/col-german-ma.html",
    },
    {
        "id": "col-mesaas-ma",
        "s": "col",
        "n": "Middle Eastern, South Asian, and African Studies (MA)",
        "en": "Master of Arts in Middle Eastern, South Asian, and African Studies (MESAAS, GSAS)",
        "deg": "MA",
        "lvl": "m",
        "stem": 0,
        "dur": "Full-time; New York NY; on path to PhD (separate PhD application required)",
        "ddl": "Fall admission: Feb 4, 2027; PhD: Dec 2, 2026",
        "lang": "TOEFL / IELTS required for non-native English speakers (GSAS code 2163)",
        "test": "GRE General: No; 3 letters (at least 2 academic); statement of academic purpose; personal statement; writing sample 15-20 pp double-spaced; research colloquium required for second/third year",
        "tui": "待补 (see GSAS tuition; MA self-funded)",
        "url": "details/col-mesaas-ma.html",
    },
    {
        "id": "nyu-cinema-ma",
        "s": "nyu",
        "n": "Cinema Studies (MA)",
        "en": "Master of Arts in Cinema Studies (Tisch School of the Arts, Department of Cinema Studies)",
        "deg": "MA",
        "lvl": "m",
        "stem": 0,
        "dur": "On-campus, Fall admission, New York NY",
        "ddl": "December 1 (Fall admission, 11:59PM ET)",
        "lang": "TOEFL / IELTS required (Tisch international admissions rules apply)",
        "test": "GRE not required; 2 letters of recommendation; professional CV; 2-3 pp statement of purpose; writing sample 15-20 pp double-spaced (film/TV/new media critical-theoretical-historical thinking; arts/humanities acceptable if no moving-image topic)",
        "tui": "待补 (see Tisch tuition)",
        "url": "details/nyu-cinema-ma.html",
    },
    {
        "id": "nyu-hebrew-ma",
        "s": "nyu",
        "n": "Hebrew and Judaic Studies (MA)",
        "en": "Master of Arts in Hebrew and Judaic Studies (GSAS Skirball Department of Hebrew and Judaic Studies)",
        "deg": "MA",
        "lvl": "m",
        "stem": 0,
        "dur": "On-campus, Fall admission only (also Spring Nov 1), New York NY; dual MA/Museum Studies and MA/MPA (Wagner) tracks available",
        "ddl": "MA: Mar 1 (Fall); Nov 1 (Spring); Dual MA/MPA: Apr 15 with funding / Jun 1 final; PhD: Jan 4",
        "lang": "TOEFL / IELTS (GSAS code 2596); recommends TOEFL 100 / IELTS 7",
        "test": "GRE not required; $110 application fee; 3 letters; statement of academic purpose; Hebrew proficiency required; two research languages for PhD track",
        "tui": "待补 (see GSAS tuition)",
        "url": "details/nyu-hebrew-ma.html",
    },
    {
        "id": "col-religion-ma",
        "s": "col",
        "n": "Religion (MA, free-standing)",
        "en": "Master of Arts in Religion (GSAS Department of Religion, free-standing)",
        "deg": "MA",
        "lvl": "m",
        "stem": 0,
        "dur": "Full-time or part-time; New York NY; dual MA/MS with Journalism available",
        "ddl": "Fall admission: May 6, 2027 (Early: Mar 18, 2027); Spring admission: Oct 29, 2026; PhD: Dec 10, 2026",
        "lang": "TOEFL / IELTS required for non-native English speakers (GSAS code 2163)",
        "test": "GRE General: No; 3 letters (at least 2 academic); CV; writing sample 10-15 pp double-spaced; MA/MPhil/PhD track; BA/MA 4+1 pathway available for Columbia undergrads",
        "tui": "待补 (see GSAS tuition; MA self-funded)",
        "url": "details/col-religion-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 / Tisch 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 = [
    ("col-german-ma.html", "Columbia University", "GSAS Department of Germanic Languages"),
    ("col-mesaas-ma.html", "Columbia University", "GSAS Department of Middle Eastern, South Asian & African Studies"),
    ("nyu-cinema-ma.html", "NYU", "Tisch School of the Arts, Department of Cinema Studies"),
    ("nyu-hebrew-ma.html", "NYU", "GSAS Skirball Department of Hebrew and Judaic Studies"),
    ("col-religion-ma.html", "Columbia University", "GSAS Department of Religion"),
]

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