# api/routers/invoices.py from fastapi import APIRouter, Depends, UploadFile, File, HTTPException, status, Form from typing import Dict, Optional from api.dependencies import get_current_active_user from services import invoice_processor_service from db.models import User router = APIRouter() ALLOWED_CONTENT_TYPES = ["application/pdf", "image/jpeg", "image/png", "image/tiff"] @router.post("/upload", response_model=Dict[str, str]) async def upload_invoice( current_user: User = Depends(get_current_active_user), file: UploadFile = File(...), # Nuevo parĂ¡metro: viene del formulario, es opcional y debe estar entre 0.0 y 1.0 confidence_threshold: Optional[float] = Form(None, ge=0.0, le=1.0) ): """ Endpoint para subir una factura. Ahora acepta un umbral de confianza opcional. """ if file.content_type not in ALLOWED_CONTENT_TYPES: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=f"Tipo de archivo no soportado. Permitidos: {', '.join(ALLOWED_CONTENT_TYPES)}" ) try: file_bytes = await file.read() # Pasamos el umbral recibido al servicio extracted_data = invoice_processor_service.process_invoice_from_bytes( file_bytes=file_bytes, mime_type=file.content_type, override_threshold=confidence_threshold ) return extracted_data except Exception as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Error al procesar la factura: {e}" )