"""
FastAPI routes for Reporte de Vencimiento.
"""
import logging
from typing import Any

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

from core.database import get_core_db
from core.security import get_current_user, validate_access_to_resource

from .schemas import VencimientoFilter
from .service import VencimientoReportService

logger = logging.getLogger(__name__)

router = APIRouter(tags=["Reports - Vencimiento"])


@router.post(
    "/generate",
    summary="Generate Reporte de Vencimiento CSV",
)
def generate_vencimiento_report(
    filters: VencimientoFilter = Body(...),
    company_id: int = Query(..., description="Company ID"),
    db: Session = Depends(get_core_db),
    current_user: Any = Depends(get_current_user),
) -> StreamingResponse:
    tenant_id = validate_access_to_resource(db, company_id, current_user)
    filters.company_id = company_id
    filters.tenant_id = tenant_id
    service = VencimientoReportService()
    return service.generate_csv_response(db=db, filters=filters)
