"""User-facing roofing invoice parity harness."""

from pathlib import Path

import fullbleed


ROOT = Path(__file__).resolve().parent
TEMPLATES = ROOT / "templates"
OUTPUT = ROOT / "output"


def create_engine(debug_out: str | None = None, perf_out: str | None = None):
    bundle = fullbleed.AssetBundle()
    bundle.add_file(str(ROOT / "vendor/css/bootstrap.min.css"), "css", name="bootstrap")
    bundle.add_file(str(ROOT / "vendor/fonts/Inter-Variable.ttf"), "font", name="inter")

    engine = fullbleed.PdfEngine(
        page_width="8.5in",
        page_height="11in",
        margin="0in",
        pdf_version="1.7",
        jit_mode="plan",
        debug=bool(debug_out),
        debug_out=debug_out,
        perf=bool(perf_out),
        perf_out=perf_out,
    )
    engine.register_bundle(bundle)
    return engine


def main():
    OUTPUT.mkdir(exist_ok=True)
    html = (TEMPLATES / "invoice.html").read_text(encoding="utf-8")
    css = (TEMPLATES / "invoice.css").read_text(encoding="utf-8")

    jit_path = OUTPUT / "roofing_invoice.jit.jsonl"
    perf_path = OUTPUT / "roofing_invoice.perf.jsonl"
    out_pdf = OUTPUT / "roofing_invoice.pdf"

    engine = create_engine(debug_out=str(jit_path), perf_out=str(perf_path))
    bytes_written = engine.render_pdf_to_file(html, css, str(out_pdf))
    print(f"Generated {out_pdf} ({bytes_written} bytes)")
    print(f"JIT log: {jit_path}")
    print(f"Perf log: {perf_path}")


if __name__ == "__main__":
    main()
