# -*- coding: utf-8 -*-
"""Round 230: batch add 5 Health Policy / Public Health PhD
- ucla-hpm-phd (UCLA Fielding PhD Health Policy and Management)
- berkeley-health-policy-phd (UC Berkeley PhD Health Policy, Graduate Group)
- umich-hsop-phd (UMich PhD Health Services Organization & Policy, HSOP)
- col-hpm-phd (Columbia Mailman PhD Health Policy and Management)
- unc-hpm-phd (UNC Gillings PhD Health Policy and Management)
Key finding: public health school Health Policy PhD programs have broadly gone GRE-free:
UCLA optional, Berkeley optional (strongly recommended), UMich explicitly no GRE reviewed,
Columbia optional, UNC explicitly neither required nor recommended.
This contrasts sharply with business school PhDs (which universally require GRE/GMAT).
Deadlines: all 5 are Dec 1, 2026.
All fully funded (PhD). UNC HPM explicitly NOT STEM-designated.
"""
import os, re

BASE = os.path.dirname(os.path.abspath(__file__))
DET = os.path.join(BASE, "details")
os.makedirs(DET, exist_ok=True)

PROGRAMS = [
    {
        "id": "ucla-hpm-phd",
        "s": "ucla",
        "n": "卫生政策与管理博士",
        "en": "PhD in Health Policy and Management (UCLA Fielding School of Public Health)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 0,
        "dur": "4-5 years full-time; fall-only admission; requires both SOPHAS and UCLA Division of Graduate Education applications",
        "ddl": "Fall 2027: December 1, 2026 (priority deadline). Late applications considered but with reduced funding/housing opportunities. Fall entry only.",
        "lang": "TOEFL or IELTS required for non-native English speakers; TOEFL SOPHAS code 5688. International transcripts require WES evaluation.",
        "test": {"cls": "GRE OPTIONAL per UCLA Graduate Programs page (Health Policy and Management, code 007I: GRE Optional). ETS code 4225 (Fielding SPH via SOPHAS). No minimum score. Note: HPM PhD applicants must submit a writing sample."},
        "tui": "Full funding package for admitted PhD students (tuition + stipend + health insurance). Contact UCLA Fielding HPM PhD office for current stipend details.",
        "url": "details/ucla-hpm-phd.html",
    },
    {
        "id": "berkeley-health-policy-phd",
        "s": "berkeley",
        "n": "卫生政策博士",
        "en": "PhD in Health Policy (UC Berkeley Graduate Group in Health Policy; interdisciplinary with Economics, Political Science, Sociology, Haas School of Business, Goldman School of Public Policy)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 0,
        "dur": "5-6 years full-time; fall-only admission; three tracks: Health Economics, Organizations & Management, Population Health Sciences",
        "ddl": "Fall 2027: December 1, 2026. Fall entry only.",
        "lang": "TOEFL or IELTS required per Berkeley Graduate Division.",
        "test": {"cls": "GRE OPTIONAL (strongly recommended for Health Policy PhD per Berkeley SPH GRE Requirement Chart). ETS code 4833 (Berkeley Graduate Organizations; no department code needed). No minimum score; holistic review. At-home test scores accepted. Scores must be within 5 years."},
        "tui": "Full funding package for admitted PhD students (tuition + stipend + health insurance). Contact Berkeley Health Policy Graduate Group for current stipend details.",
        "url": "details/berkeley-health-policy-phd.html",
    },
    {
        "id": "umich-hsop-phd",
        "s": "umich",
        "n": "卫生服务组织与政策博士",
        "en": "PhD in Health Services Organization and Policy (HSOP, University of Michigan School of Public Health, Rackham Graduate School)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 0,
        "dur": "4-5 years full-time; 1-2 years coursework + 2-5 years independent research; fall-only admission; admitted students notified late January/early February, visit day late February/early March",
        "ddl": "Fall 2027: December 1, 2026. Fall entry only. Late or incomplete applications will not be reviewed.",
        "lang": "TOEFL or IELTS required per Rackham Graduate School.",
        "test": {"cls": "GRE NOT REQUIRED and NOT REVIEWED (per Michigan Public Health: 'does not require and does not review GRE or other standardized test scores for admission to any master's or doctoral programs'). No GRE scores will be considered. Holistic review based on grades, key coursework, essays, experience, and letters."},
        "tui": "Full funding package for admitted PhD students (tuition + stipend + health insurance) through Rackham and the School of Public Health.",
        "url": "details/umich-hsop-phd.html",
    },
    {
        "id": "col-hpm-phd",
        "s": "col",
        "n": "卫生政策与管理博士",
        "en": "PhD in Health Policy and Management (Columbia University Mailman School of Public Health)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 0,
        "dur": "4-5 years full-time; fall-only admission; applications submitted via SOPHAS",
        "ddl": "Fall 2027: December 1, 2026 (final deadline for all doctoral applications). Fall entry only.",
        "lang": "TOEFL, IELTS, or Duolingo English Test required for non-native English speakers.",
        "test": {"cls": "GRE OPTIONAL per Columbia Mailman FAQ (standardized test scores optional for many programs; confirm with HPM department). ETS code 2162 (Columbia GSAS; department code 0000). No minimum score. Official scores must be received within two weeks of application deadline."},
        "tui": "Full funding package for admitted PhD students (tuition + stipend + health insurance). Contact Columbia Mailman HPM PhD office for current stipend details.",
        "url": "details/col-hpm-phd.html",
    },
    {
        "id": "unc-hpm-phd",
        "s": "unc",
        "n": "卫生政策与管理博士",
        "en": "PhD in Health Policy and Management (UNC Gillings School of Global Public Health)",
        "deg": "PhD",
        "lvl": "d",
        "stem": 0,
        "dur": "4-5 years expected duration; fall-only admission; applications via SOPHAS + UNC Graduate School; NOT STEM-designated per UNC Gillings",
        "ddl": "Fall 2027: December 1, 2026. Fall entry only. (Note: MSPH in HPM admissions paused for Fall 2027; PhD still admitting.)",
        "lang": "TOEFL, IELTS, or Duolingo English Test required for international applicants; scores no more than 2 years old. TOEFL code 5816 routes to UNC.",
        "test": {"cls": "GRE NOT REQUIRED and NOT TYPICALLY RECOMMENDED (per UNC Gillings: 'GRE scores are neither required nor typically recommended for applicants to the PhD Program'). No GRE scores considered. Minimum undergraduate GPA 3.0 (B). Master's degree or health policy/health services research experience preferred but not required."},
        "tui": "Full funding package for admitted PhD students (tuition + stipend + health insurance). Contact UNC Gillings HPM PhD Director of Admissions for current stipend details.",
        "url": "details/unc-hpm-phd.html",
    },
]

DETAIL_TEMPLATE = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{school} - {en}</title>
<style>
body{{font-family:-apple-system,'Segoe UI',Roboto,sans-serif;max-width:960px;margin:24px auto;padding:0 20px;color:#222;line-height:1.6}}
h1{{font-size:24px;border-bottom:2px solid #1a73e8;padding-bottom:8px}}
h2{{font-size:18px;color:#1a73e8;margin-top:24px}}
.card{{background:#f8f9fa;border-left:4px solid #1a73e8;padding:12px 16px;margin:12px 0;border-radius:4px}}
.meta{{display:grid;grid-template-columns:120px 1fr;gap:6px 12px;font-size:14px}}
.meta b{{color:#555}}
ul{{margin:6px 0;padding-left:20px}}
.note{{background:#fff3cd;border-left-color:#ffc107;padding:10px 14px;margin-top:16px;font-size:13px}}
a{{color:#1a73e8}}
</style>
</head>
<body>
<h1>{school} - {en}</h1>
<div class="card">
<div class="meta">
<b>Degree</b><span>{deg}</span>
<b>Level</b><span>Research Doctorate (PhD)</span>
<b>STEM</b><span>{stem_label}</span>
<b>Duration</b><span>{dur}</span>
<b>Deadline</b><span>{ddl}</span>
<b>Language</b><span>{lang}</span>
<b>Standardized</b><span>{test_cls}</span>
<b>Funding</b><span>{tui}</span>
</div>
</div>

<h2>Program Overview</h2>
<div class="card">
{overview}
</div>

<h2>Admissions Requirements</h2>
<div class="card">
{requirements}
</div>

<h2>Research Areas</h2>
<div class="card">
{research}
</div>

<h2>Official Source</h2>
<div class="card">
{source}
</div>

<div class="note">
<strong>Note:</strong> This is a research-oriented PhD program in health policy and management at a top-20 U.S. school of public health. Admitted PhD students receive full financial support. Admission statistics shown are distribution ranges, not minimum thresholds. Course/credit details pending department bulletin verification. STEM designation status per department and CIP code.
</div>
</body>
</html>
"""

PAGES = {
    "ucla-hpm-phd": {
        "school": "UCLA Fielding School of Public Health",
        "en": "PhD in Health Policy and Management",
        "deg": "Doctor of Philosophy (PhD)",
        "stem_label": "Pending department confirmation",
        "dur": "4-5 years full-time; fall-only; dual SOPHAS + UCLA DGE application",
        "ddl": "Fall 2027: December 1, 2026 (priority). Late applications considered with reduced funding/housing.",
        "lang": "TOEFL/IELTS required (non-native English); TOEFL SOPHAS code 5688; WES evaluation for international transcripts.",
        "test_cls": "GRE OPTIONAL (UCLA Graduate Programs code 007I: GRE Optional). ETS 4225 via SOPHAS. No minimum score. Writing sample required for HPM PhD.",
        "tui": "Full funding for admitted PhD students (tuition + stipend + health insurance).",
        "overview": "UCLA Fielding PhD in Health Policy and Management emphasizes new ideas, theories, and practices that contribute to improving population health. The program trains scholars for academic careers in research and teaching on health care delivery, health policy, and health management. HPM is one of five departments within the UCLA Fielding School of Public Health. The program requires both a SOPHAS application and a UCLA Division of Graduate Education application; the DGE application is pre-populated based on the SOPHAS submission.",
        "requirements": "<ul><li>Bachelor's degree; strong quantitative/social science preparation</li><li>GRE optional (no minimum score)</li><li>TOEFL/IELTS (non-native English); WES for international transcripts</li><li>Statement of purpose, personal statement, CV/resume</li><li>3 letters of recommendation (at least 2 from professors; 1 may be from employer; if out of school >5 years, 3 employer letters acceptable)</li><li>Writing sample required for HPM PhD applicants</li><li>December 1, 2026 priority deadline</li><li>Fall entry only; full-time</li><li>Master's degree in public health or related field with 3.5+ GPA viewed favorably but not required</li><li>Work experience in the field viewed favorably</li></ul>",
        "research": "<ul><li>Health care organization and management</li><li>Health policy and politics</li><li>Health economics and financing</li><li>Health services research</li><li>Long-term care and aging services</li><li>Mental health policy</li><li>Health disparities and social determinants of health</li><li>Cross-disciplinary work with UCLA Anderson, UCLA Law, and UCLA Health</li></ul>",
        "source": '<a href="https://ph.ucla.edu/taxonomy/term/173" target="_blank">UCLA Fielding PhD HPM (Dec 1, 2026)</a> | <a href="https://ph.ucla.edu/node/1583" target="_blank">HPM PhD How to Apply (writing sample, ETS 4225)</a> | <a href="https://grad.ucla.edu/gasaa/deptinfo/deptinfo.asp?academicyear=20262027&code=007I" target="_blank">UCLA Graduate Programs: HPM GRE Optional</a>',
    },
    "berkeley-health-policy-phd": {
        "school": "UC Berkeley Graduate Group in Health Policy",
        "en": "PhD in Health Policy",
        "deg": "Doctor of Philosophy (PhD)",
        "stem_label": "Pending department confirmation",
        "dur": "5-6 years full-time; fall-only; three tracks: Health Economics, Organizations & Management, Population Health Sciences",
        "ddl": "Fall 2027: December 1, 2026. Fall entry only.",
        "lang": "TOEFL/IELTS required per Berkeley Graduate Division.",
        "test_cls": "GRE OPTIONAL (strongly recommended for Health Policy PhD per Berkeley SPH chart). ETS 4833. No minimum score; holistic. At-home tests accepted. Scores within 5 years.",
        "tui": "Full funding for admitted PhD students (tuition + stipend + health insurance).",
        "overview": "UC Berkeley PhD in Health Policy is an interdisciplinary program that applies social and behavioral science disciplines to real-world health issues. Students select a specialty track from among three options (Health Economics, Organizations & Management, or Population Health Sciences) while receiving rigorous training in quantitative research methods. The program draws on faculty from UC Berkeley's top-ranked Economics, Political Science, and Sociology departments, as well as the Haas School of Business and the Goldman School of Public Policy. Graduates assume academic careers in research and teaching.",
        "requirements": "<ul><li>Bachelor's degree; strong quantitative preparation</li><li>GRE optional (strongly recommended; no minimum score)</li><li>TOEFL/IELTS (non-native English)</li><li>Statement of purpose, transcripts, CV</li><li>Letters of recommendation</li><li>December 1, 2026 deadline</li><li>Fall entry only; full-time</li><li>Minimum GPA 3.0 (B) on 4.0 scale</li><li>Select one of three tracks in application</li><li>Note: MD/PhD-equivalent degree holders may be exempt from GRE per Berkeley SPH exemption policy</li></ul>",
        "research": "<ul><li>Health economics and econometrics (Health Economics track)</li><li>Health care organizations, management, and policy (Organizations & Management track)</li><li>Population health sciences and epidemiology (Population Health Sciences track)</li><li>Health insurance and public insurance programs</li><li>Health technology assessment and cost-effectiveness</li><li>Cross-disciplinary work with Berkeley Economics, Political Science, Sociology, Haas, and Goldman</li></ul>",
        "source": '<a href="https://grad.berkeley.edu/program/health-policy-phd/" target="_blank">Berkeley Graduate Division: Health Policy PhD (Dec 1, GRE Optional, 3 tracks)</a> | <a href="https://publichealth.berkeley.edu/admissions/on-campus-non-sophas" target="_blank">Berkeley SPH Non-SOPHAS Admissions (GRE chart, ETS 4833)</a>',
    },
    "umich-hsop-phd": {
        "school": "University of Michigan School of Public Health",
        "en": "PhD in Health Services Organization and Policy (HSOP)",
        "deg": "Doctor of Philosophy (PhD)",
        "stem_label": "Pending department confirmation",
        "dur": "4-5 years full-time; 1-2 years coursework + 2-5 years independent research; fall-only; Rackham Graduate School",
        "ddl": "Fall 2027: December 1, 2026. Fall entry only. Late/incomplete applications not reviewed. Decisions late January/early February; visit day late February/early March.",
        "lang": "TOEFL/IELTS required per Rackham Graduate School.",
        "test_cls": "GRE NOT REQUIRED and NOT REVIEWED (Michigan Public Health explicitly: 'does not require and does not review GRE or other standardized test scores for any master's or doctoral programs'). No GRE scores considered. Holistic review: grades, key coursework, essays, experience, letters.",
        "tui": "Full funding for admitted PhD students through Rackham + SPH (tuition + stipend + health insurance).",
        "overview": "The Health Services Organization and Policy (HSOP) PhD program at the University of Michigan School of Public Health is the department's doctoral program that prepares students for careers in research, teaching, and policy analysis in health services and policy. The program typically consists of 1-2 years of full-time coursework followed by 2-5 years of full-time independent research. HSOP is one of several doctoral programs within the Michigan Public Health portfolio (alongside Biostatistics, Environmental Health Sciences, Nutritional Sciences, Health Behavior & Health Education, and Health Management & Policy).",
        "requirements": "<ul><li>Bachelor's degree; strong preparation</li><li>GRE NOT required and NOT reviewed (no scores considered)</li><li>TOEFL/IELTS (non-native English) per Rackham</li><li>Statement of purpose, transcripts, CV</li><li>Letters of recommendation</li><li>December 1, 2026 deadline (all supplemental materials must be received by Dec 1)</li><li>Fall entry only; full-time</li><li>Holistic review: no single factor determines admission</li><li>Applications through Rackham Graduate School ApplyWeb</li><li>Indicate intended cognate on application</li></ul>",
        "research": "<ul><li>Health care organization and management</li><li>Health services research and delivery science</li><li>Health policy and politics</li><li>Health economics and financing</li><li>Long-term care and aging</li><li>Mental health and substance use policy</li><li>Health disparities and equity</li><li>Cross-disciplinary work with Michigan Ross, Ford School, and Institute for Healthcare Policy and Innovation (IHPI)</li></ul>",
        "source": '<a href="https://www1.sph.umich.edu/hmp/programs/phd.html" target="_blank">UMich HSOP PhD (Dec 1, 2026)</a> | <a href="https://sph.umich.edu/admissions/applications-deadlines.html/" target="_blank">Michigan Public Health Deadlines (no GRE reviewed)</a> | <a href="https://www1.sph.umich.edu/admissions/programs-degrees/doctoral/degrees-and-requirements.html" target="_blank">SPH Doctoral Degrees and Requirements</a>',
    },
    "col-hpm-phd": {
        "school": "Columbia University Mailman School of Public Health",
        "en": "PhD in Health Policy and Management",
        "deg": "Doctor of Philosophy (PhD)",
        "stem_label": "Pending department confirmation",
        "dur": "4-5 years full-time; fall-only; SOPHAS application",
        "ddl": "Fall 2027: December 1, 2026 (final deadline for all doctoral applications). Fall entry only.",
        "lang": "TOEFL, IELTS, or Duolingo English Test required for non-native English speakers.",
        "test_cls": "GRE OPTIONAL (per Columbia Mailman FAQ: standardized test scores optional for many programs; confirm with HPM department). ETS 2162 (GSAS; department code 0000). No minimum score. Official scores within 2 weeks of deadline.",
        "tui": "Full funding for admitted PhD students (tuition + stipend + health insurance).",
        "overview": "Columbia Mailman School of Public Health offers a PhD in Health Policy and Management as one of its doctoral programs (alongside DrPH, and PhDs in Biostatistics, Epidemiology, Environmental Health Sciences, Health Policy and Management, Population and Family Health, Sociomedical Sciences, and Health Education). Doctoral applications are submitted via SOPHAS and due December 1 (the final deadline for all doctoral programs). The program prepares scholars for academic careers in health policy research, health services management, and health economics.",
        "requirements": "<ul><li>Bachelor's degree; strong quantitative/social science preparation</li><li>GRE optional (confirm with HPM department)</li><li>TOEFL/IELTS/Duolingo (non-native English)</li><li>Statement of purpose / personal statement, transcripts, CV</li><li>Evidence of quantitative and analytical ability (via coursework or application assessment)</li><li>Letters of recommendation</li><li>December 1, 2026 final deadline for doctoral applications</li><li>Fall entry only; full-time</li><li>Applications via SOPHAS</li><li>No minimum score; holistic review</li></ul>",
        "research": "<ul><li>Health policy and politics</li><li>Health care organization and management</li><li>Health economics and financing</li><li>Health services research</li><li>Mental health policy</li><li>Health disparities and social determinants</li><li>Global health policy</li><li>Cross-disciplinary work with Columbia Business School, Columbia Law, and Columbia Medical Center</li></ul>",
        "source": '<a href="https://www.publichealth.columbia.edu/become-student/how-apply/deadlines" target="_blank">Columbia Mailman Deadlines (Dec 1 final for doctoral)</a> | <a href="https://www.publichealth.columbia.edu/about-us/frequently-asked-questions" target="_blank">Mailman FAQ (GRE optional for many programs)</a> | <a href="https://www.gsas.columbia.edu/content/admissions-supporting-materials" target="_blank">Columbia GSAS Supporting Materials (ETS 2162)</a>',
    },
    "unc-hpm-phd": {
        "school": "UNC Gillings School of Global Public Health",
        "en": "PhD in Health Policy and Management",
        "deg": "Doctor of Philosophy (PhD)",
        "stem_label": "Not STEM-designated (per UNC Gillings)",
        "dur": "4-5 years expected; fall-only; SOPHAS + UNC Graduate School; NOT STEM-designated",
        "ddl": "Fall 2027: December 1, 2026. Fall entry only. (Note: MSPH in HPM admissions paused for Fall 2027; PhD still admitting.)",
        "lang": "TOEFL/IELTS/Duolingo required for international applicants; scores no more than 2 years old. TOEFL code 5816.",
        "test_cls": "GRE NOT REQUIRED and NOT TYPICALLY RECOMMENDED (UNC Gillings: 'GRE scores are neither required nor typically recommended for applicants to the PhD Program'). No GRE scores considered. Minimum undergraduate GPA 3.0 (B). Master's degree or HSR experience preferred but not required.",
        "tui": "Full funding for admitted PhD students (tuition + stipend + health insurance).",
        "overview": "The UNC Gillings PhD in Health Policy and Management is a residential doctoral program housed in the top-rated Gillings School's Department of Health Policy and Management. During the first two years, students take courses in health services research, research design, quantitative and qualitative methods, and health policy. The program's mission is to provide students with the skills to conduct innovative, high-impact health services and health policy research. The PhD is distinct from the DrPH in Public Health Executive Leadership (online, Dec 15 deadline). Note: the MSPH in HPM is paused for Fall 2027, but the PhD is still admitting.",
        "requirements": "<ul><li>Bachelor's degree; minimum GPA 3.0 (B) on 4.0 scale</li><li>GRE NOT required and NOT typically recommended (no scores considered)</li><li>TOEFL/IELTS/Duolingo (international; scores within 2 years); TOEFL code 5816</li><li>Statement of purpose, transcripts, CV</li><li>3 letters of recommendation (academic and/or professional)</li><li>December 1, 2026 deadline</li><li>Fall entry only; full-time residential</li><li>Master's degree or health policy/health services research experience preferred but not required</li><li>Applications via SOPHAS + UNC Graduate School</li><li>Program is NOT STEM-designated per UNC Gillings</li></ul>",
        "research": "<ul><li>Health services research and delivery science</li><li>Health policy and politics</li><li>Health care organization and management</li><li>Health economics and financing</li><li>Long-term care and aging</li><li>Mental health and substance use policy</li><li>Health disparities and equity</li><li>Professional development emphasis: rigorous training in research and analytical methods</li></ul>",
        "source": '<a href="https://catalog.unc.edu/graduate/schools-departments/health-policy-management/" target="_blank">UNC Catalog: HPM Department (PhD)</a> | <a href="http://www.shturl.cc/90c67462a44592bb2b236244e9ac1446" target="_blank">UNC Gillings PhD HPM (Dec 1, NOT STEM, GRE not required)</a> | <a href="http://www.shturl.cc/9cbfa979dfbc1020b9d49ce45010f69a" target="_blank">UNC HPM Test Requirements (GRE not required, TOEFL 5816)</a>',
    },
}

# Write detail pages
for p in PROGRAMS:
    key = p["id"]
    d = PAGES[key]
    html = DETAIL_TEMPLATE.format(
        school=d["school"], en=d["en"], deg=d["deg"], stem_label=d["stem_label"],
        dur=d["dur"], ddl=d["ddl"], lang=d["lang"], test_cls=d["test_cls"], tui=d["tui"],
        overview=d["overview"], requirements=d["requirements"],
        research=d["research"], source=d["source"],
    )
    path = os.path.join(DET, key + ".html")
    with open(path, "w", encoding="utf-8") as f:
        f.write(html)
    print(f"Wrote: {key}.html")

# Append PROGRAMS
idx_path = os.path.join(BASE, "index_main.html")
with open(idx_path, "r", encoding="utf-8") as f:
    html = f.read()

def build_entry(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_cls}"}},'
            'tui:"{tui}",url:"{url}"}}').format(
        id=p["id"], s=p["s"], n=p["n"], en=p["en"], deg=p["deg"], lvl=p["lvl"],
        stem=p["stem"], dur=p["dur"], ddl=p["ddl"], lang=p["lang"],
        test_cls=p["test"]["cls"].replace('"', "'"),
        tui=p["tui"].replace('"', "'"), url=p["url"],
    )

new_entries = ",\n".join(build_entry(p) for p in PROGRAMS)

m = re.search(r'const\s+PROGRAMS\s*=\s*\[', html)
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_pos = i - 1
prefix = html[:insert_pos].rstrip()
suffix = html[insert_pos:]
if prefix.endswith('{'):
    new_block = prefix + "\n" + new_entries + "\n" + suffix
else:
    new_block = prefix + ",\n" + new_entries + "\n" + suffix

# Append VD on new_block (re-locate to avoid offset)
vd_entries = ",\n".join(f'"{p["url"]}"' for p in PROGRAMS)
m2 = re.search(r'VERIFIED_DETAILS\s*=\s*new\s+Set\s*\(\s*\[', new_block)
vstart = m2.end()
vdepth = 1
j = vstart
while j < len(new_block) and vdepth > 0:
    c = new_block[j]
    if c == '[': vdepth += 1
    elif c == ']': vdepth -= 1
    j += 1
vinsert_pos = j - 1
vprefix = new_block[:vinsert_pos].rstrip()
vsuffix = new_block[vinsert_pos:]
if vprefix.endswith(','):
    new_block = vprefix + "\n" + vd_entries + "\n" + vsuffix
elif vprefix.endswith('['):
    new_block = vprefix + "\n" + vd_entries + "\n" + vsuffix
else:
    new_block = vprefix + ",\n" + vd_entries + "\n" + vsuffix

with open(idx_path, "w", encoding="utf-8") as f:
    f.write(new_block)

print(f"Appended {len(PROGRAMS)} PROGRAMS entries and {len(PROGRAMS)} VD entries")
print(f"Done. File size: {os.path.getsize(idx_path)}")
