from fastapi import APIRouter, Depends, HTTPException, status

from app.api.deps import RequestContext, get_request_context
from app.models.firewall import FirewallResponse, SecurityCheckRequest
from app.services.firewall import ContentFilteredError, FirewallError, run_firewall_check

router = APIRouter()


@router.post("/check", response_model=FirewallResponse)
async def run_firewall_endpoint(
    payload: SecurityCheckRequest,
    ctx: RequestContext = Depends(get_request_context),
) -> FirewallResponse:
    try:
        result = run_firewall_check(payload, user_id=ctx.user_id, source=ctx.auth_scheme)
        return result
    except ContentFilteredError as exc:
        raise HTTPException(
            status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
            detail="Content was filtered by provider",
        ) from exc
    except FirewallError 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
