"""Tests for :mod:`apps.core.utils.pdf`.

WeasyPrint needs native system deps (Pango, GTK). When those are missing we
skip the actual render tests but still assert the module imports and fails
loudly *only* at call site.
"""
from __future__ import annotations

import pytest

from apps.core.utils import pdf as pdf_module


def _weasyprint_available() -> bool:
    try:
        import weasyprint  # noqa: F401
    except Exception:  # pragma: no cover - environment-specific
        return False
    return True


def test_module_imports_even_when_weasyprint_binaries_missing():
    # Successful import of the helper module is the guarantee we rely on.
    assert callable(pdf_module.render_pdf)
    assert callable(pdf_module.render_pdf_from_html)
    assert callable(pdf_module.pdf_response)


@pytest.mark.skipif(not _weasyprint_available(), reason="WeasyPrint native deps missing")
def test_render_pdf_from_html_produces_valid_pdf_bytes():
    html = "<html><body><h1>Hello</h1></body></html>"
    data = pdf_module.render_pdf_from_html(html)
    # PDF magic number.
    assert data[:4] == b"%PDF"


@pytest.mark.skipif(not _weasyprint_available(), reason="WeasyPrint native deps missing")
def test_pdf_response_sets_headers():
    html = "<html><body>x</body></html>"
    data = pdf_module.render_pdf_from_html(html)
    assert data[:4] == b"%PDF"
