from html import escape
from pathlib import Path

source = Path('dokumen/Laporan_FS_CMU_GoogleDocs_Rapih.md')
out_dir = Path('html')
out_file = out_dir / 'bab-02-gambaran-umum-dan-analisis-situasi-eksternal.html'

text = source.read_text(encoding='utf-8')
lines = text.splitlines()

start = None
end = None
for i, line in enumerate(lines):
    if line.strip() == 'BAB 2 GAMBARAN UMUM DAN ANALISIS SITUASI EKSTERNAL':
        # Use second occurrence after daftar isi; first content occurrence has following 2.1 body.
        if i + 2 < len(lines) and lines[i + 2].strip().startswith('2.1 '):
            start = i
            break
if start is None:
    raise SystemExit('BAB 2 content not found')

for i in range(start + 1, len(lines)):
    if lines[i].strip().startswith('BAB 3 '):
        end = i
        break
if end is None:
    end = len(lines)

chapter_lines = lines[start:end]

body = []
i = 0
while i < len(chapter_lines):
    stripped = chapter_lines[i].strip()
    if not stripped:
        i += 1
        continue

    if stripped.startswith('|') and '|' in stripped[1:]:
        rows = []
        while i < len(chapter_lines) and chapter_lines[i].strip().startswith('|'):
            row = [c.strip() for c in chapter_lines[i].strip().strip('|').split('|')]
            is_separator = all(set(c.replace(':', '').replace('-', '').strip()) == set() for c in row)
            if not is_separator:
                rows.append(row)
            i += 1
        if rows:
            body.append('<div class="table-wrap"><table>')
            for r, row in enumerate(rows):
                tag = 'th' if r == 0 else 'td'
                body.append('<tr>' + ''.join(f'<{tag}>{escape(c)}</{tag}>' for c in row) + '</tr>')
            body.append('</table></div>')
        continue

    if stripped.startswith('BAB 2'):
        body.append(f'<h1>{escape(stripped)}</h1>')
    elif stripped.startswith('Tabel '):
        body.append(f'<p class="caption">{escape(stripped)}</p>')
    elif stripped[:3].count('.') == 1 and stripped[0].isdigit() and ' ' in stripped:
        body.append(f'<h2>{escape(stripped)}</h2>')
    elif stripped[:5].count('.') >= 2 and stripped[0].isdigit() and ' ' in stripped:
        body.append(f'<h3>{escape(stripped)}</h3>')
    else:
        body.append(f'<p>{escape(stripped)}</p>')
    i += 1

html = '''<!doctype html>
<html lang="id">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>BAB 2 - Gambaran Umum dan Analisis Situasi Eksternal</title>
  <style>
    @page { size: A4; margin: 2cm 1.8cm; }
    :root { --ink:#172033; --muted:#667085; --line:#d0d5dd; --accent:#0f766e; --soft:#f0fdfa; }
    * { box-sizing: border-box; }
    body { margin:0; background:#f5f7fb; color:var(--ink); font-family: Arial, Helvetica, sans-serif; line-height:1.55; }
    .page { width:min(100%, 920px); margin:24px auto; background:white; padding:56px 64px; box-shadow:0 8px 30px rgba(16,24,40,.10); }
    .kicker { color:var(--accent); font-size:12px; letter-spacing:.12em; text-transform:uppercase; font-weight:700; text-align:center; margin-bottom:8px; }
    h1 { text-align:center; font-size:24px; line-height:1.25; margin:0 0 28px; text-transform:uppercase; }
    h2 { font-size:18px; margin:28px 0 10px; padding-left:12px; border-left:4px solid var(--accent); }
    h3 { font-size:15px; margin:22px 0 8px; color:#24324b; }
    p { margin:0 0 12px; text-align:justify; }
    .caption { text-align:center; font-weight:700; margin:22px 0 8px; color:#344054; }
    .table-wrap { overflow-x:auto; margin:0 0 18px; }
    table { border-collapse:collapse; width:100%; font-size:13px; }
    th, td { border:1px solid var(--line); padding:8px 10px; vertical-align:top; }
    th { background:var(--soft); text-align:center; font-weight:700; }
    tr:nth-child(even) td { background:#fcfcfd; }
    .note { margin-top:32px; padding:12px 14px; background:#f8fafc; border:1px solid #e4e7ec; color:var(--muted); font-size:12px; }
    @media print { body{background:white;} .page{width:auto; margin:0; padding:0; box-shadow:none;} }
  </style>
</head>
<body>
  <main class="page">
    <div class="kicker">Laporan Studi Kelayakan CMU RSUD KH Ahmad Hanafiah</div>
''' + '\n'.join(body) + '''
    <div class="note">File HTML ini fokus untuk pengembangan BAB 2. Sumber konten: dokumen/Laporan_FS_CMU_GoogleDocs_Rapih.md.</div>
  </main>
</body>
</html>
'''

out_dir.mkdir(exist_ok=True)
out_file.write_text(html, encoding='utf-8')
print(out_file)
