#!/usr/bin/env python3
"""Convert Laporan_FS_CMU_GoogleDocs_Rapih.md to PDF using markdown + WeasyPrint."""

import re

import markdown
from weasyprint import CSS, HTML

INPUT_MD = "dokumen/Laporan_FS_CMU_GoogleDocs_Rapih.md"
OUTPUT_PDF = "dokumen/Laporan_FS_CMU_RSUD_KH_Ahmad_Hanafiah.pdf"

# ---------- read source --------------------------------------------------
with open(INPUT_MD, encoding="utf-8") as f:
    md_text = f.read()

# Split into TOC section (lines 1-~230) and main body (line 231+)
# The file has two copies of content: first is a TOC/index, second is full body.
# We keep everything — markdown handles it fine.

# ---------- convert to HTML ----------------------------------------------
md = markdown.Markdown(extensions=["tables", "toc", "fenced_code"])
body_html = md.convert(md_text)

# ---------- CSS styling --------------------------------------------------
CSS_STYLE = """
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans:wght@400;600;700&display=swap');

@page {
    size: A4;
    margin: 2.5cm 2.5cm 2.5cm 2.5cm;
    @bottom-center {
        content: counter(page) " / " counter(pages);
        font-size: 9pt;
        color: #666;
    }
}

body {
    font-family: 'Noto Sans', 'Liberation Sans', Arial, sans-serif;
    font-size: 11pt;
    line-height: 1.6;
    color: #1a1a1a;
}

h1 {
    font-size: 15pt;
    font-weight: 700;
    color: #1a3a6a;
    margin-top: 2em;
    margin-bottom: 0.5em;
    page-break-before: always;
    border-bottom: 2px solid #1a3a6a;
    padding-bottom: 4px;
}

h1:first-of-type {
    page-break-before: avoid;
}

h2 {
    font-size: 13pt;
    font-weight: 700;
    color: #1a3a6a;
    margin-top: 1.5em;
    margin-bottom: 0.4em;
}

h3 {
    font-size: 11.5pt;
    font-weight: 600;
    color: #2c5282;
    margin-top: 1.2em;
    margin-bottom: 0.3em;
}

h4 {
    font-size: 11pt;
    font-weight: 600;
    color: #2c5282;
    margin-top: 1em;
    margin-bottom: 0.3em;
}

p {
    margin-bottom: 0.6em;
    text-align: justify;
}

/* Tables */
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 9pt;
    margin: 1em 0;
    page-break-inside: avoid;
}

thead tr {
    background-color: #1a3a6a;
    color: #ffffff;
}

th {
    padding: 6px 8px;
    text-align: left;
    font-weight: 600;
}

td {
    padding: 5px 8px;
    border: 1px solid #c0c8d8;
    vertical-align: top;
}

tr:nth-child(even) td {
    background-color: #f0f4fa;
}

/* Lists */
ul, ol {
    margin: 0.4em 0 0.6em 1.5em;
    padding: 0;
}

li {
    margin-bottom: 0.25em;
}

/* Code */
code {
    font-family: 'Courier New', monospace;
    font-size: 9pt;
    background: #f5f5f5;
    padding: 1px 4px;
    border-radius: 2px;
}

pre {
    background: #f5f5f5;
    padding: 0.8em;
    border-radius: 4px;
    font-size: 8.5pt;
    overflow-x: auto;
}

/* Horizontal rules */
hr {
    border: none;
    border-top: 1px solid #c0c8d8;
    margin: 1.5em 0;
}

/* Strong / bold */
strong {
    color: #1a3a6a;
}
"""

# ---------- full HTML document -------------------------------------------
html_doc = f"""<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Laporan Feasibility Study CMU RSUD KH Ahmad Hanafiah - Sukadana</title>
</head>
<body>
{body_html}
</body>
</html>"""

# ---------- render PDF ---------------------------------------------------
print("Rendering PDF …")
HTML(string=html_doc, base_url=".").write_pdf(
    OUTPUT_PDF,
    stylesheets=[CSS(string=CSS_STYLE)],
)
print(f"Done! Saved to: {OUTPUT_PDF}")
