from fastapi import APIRouter, Depends, HTTPException, status

from app.api.deps import RequestContext, get_request_context
from app.models.pii import PiiRedactionRequest, PiiRedactionResponse
from app.services.pii import PiiServiceError, redact_text

router = APIRouter()


@router.post('', response_model=PiiRedactionResponse)
async def pii_redaction_endpoint(
    payload: PiiRedactionRequest,
    ctx: RequestContext = Depends(get_request_context),
) -> PiiRedactionResponse:
    try:
        return await redact_text(payload, user_id=ctx.user_id, source=ctx.auth_scheme, api_key=ctx.api_key)
    except PiiServiceError as exc:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
    except Exception as exc:  # pragma: no cover
        raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(exc)) from exc
