"""
FastAPI route for Compliance Report PDF generation.

Endpoint:
  GET /compliance/report/pdf  — generate comprehensive compliance report as PDF
"""

import logging
from typing import Optional

from fastapi import APIRouter, Depends, Query
from fastapi.responses import StreamingResponse
from sqlalchemy.orm import Session
import io

from classroom_engine.database import get_db
from .tenant_utils import get_tenant_id as _get_tenant_id
from compliance.services.compliance_pdf_generator import CompliancePDFGenerator

logger = logging.getLogger(__name__)
router = APIRouter(prefix="/compliance/report", tags=["compliance-report"])


@router.get("/pdf")
def generate_compliance_report_pdf(
    project_id: Optional[str] = Query(None),
    language: str = Query("de"),
    db: Session = Depends(get_db),
    tenant_id: str = Depends(_get_tenant_id),
):
    """Generate a comprehensive compliance PDF report for a project."""
    generator = CompliancePDFGenerator(db)
    pdf_bytes, filename = generator.generate(tenant_id, project_id, language)
    return StreamingResponse(
        io.BytesIO(pdf_bytes),
        media_type="application/pdf",
        headers={"Content-Disposition": f'attachment; filename="{filename}"'},
    )
