#!/usr/bin/env python3
"""Build docs/<name>.pdf for every docs/*.md (top-level only, not _pdf_build/)."""

from __future__ import annotations

import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
DOCS = ROOT / "docs"
sys.path.insert(0, str(Path(__file__).resolve().parent))

from md_to_pdf import build_pdf_from_markdown  # noqa: E402

SUBTITLE = {
    "20260602_REVERSE_SSH_proposals_summary.md": "Generated from markdown — rev. 1–3 + appendices WG test/production",
    "20260607-2FA-email-mobile-auth-flow.md": "Email forwarding + registration/2FA — alpha must-have",
    "20260608_ALPHA_PRIORITIES.md": "Master alpha priority plan — 2026-06-08 renumber",
    "20260608_STREAM_DUMP_DEBUG.md": "FR 0.1 stream dump while casting",
    "OPEN_TASKS_GRAPH.md": "Open tasks dependency graph — alpha critical path",
    "GIT_FLOW.md": "Branches, next, release — from markdown",
    "REMOTE_ACCESS_IMPL.md": "WireGuard v1 — implementation and deploy",
    "REMOTE_ACCESS_VALIDATION.md": "Linux CLI validation — device sim + E2E",
    "INFRA.md": "Production topology — FE, BE, ports",
    "20260610_SERVICES.md": "Exposed services catalog — URLs, APIs, validation — 2026-06-10",
}


def main() -> int:
    md_files = sorted(DOCS.glob("*.md"))
    if not md_files:
        print("No markdown files in docs/", file=sys.stderr)
        return 1
    ok = 0
    for md in md_files:
        out = md.with_suffix(".pdf")
        subtitle = SUBTITLE.get(md.name, f"From {md.name}")
        try:
            build_pdf_from_markdown(md, out, doc_subtitle=subtitle)
            ok += 1
        except Exception as e:
            print(f"FAIL {md.name}: {e}", file=sys.stderr)
    print(f"Built {ok}/{len(md_files)} PDFs under docs/")
    return 0 if ok == len(md_files) else 1


if __name__ == "__main__":
    raise SystemExit(main())
