"""
PDF generation service for executive reports.

Renders the executive PDF template without requiring an HttpRequest.
"""

from django.template.loader import render_to_string
from django.utils import timezone
from weasyprint import HTML


def generate_executive_pdf(report, user=None) -> bytes:
    """
    Generate a PDF byte stream for an ExecutiveReport.

    Args:
        report: ExecutiveReport instance
        user: User requesting the PDF (optional)

    Returns:
        PDF file contents as bytes
    """
    context = {
        "report": report,
        "narrative": report.narrative_en,
        "highlights": report.highlights_en,
        "concerns": report.concerns_en,
        "metrics": report.metrics_snapshot,
        "generated_at": timezone.now().strftime("%Y-%m-%d %H:%M"),
        "user": user,
    }

    html_string = render_to_string("executive/pdf_report.html", context)
    return HTML(string=html_string).write_pdf()
