"""EX-B2-12: the custom handler intentionally destroys useful locations."""

from typing import Annotated

from fastapi import FastAPI, Path, Query, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field

app = FastAPI(title="Validation location incident")


class Owner(BaseModel):
    name: str = Field(min_length=2)


class RecordInput(BaseModel):
    owner: Owner
    priority: int = Field(ge=1, le=5)


@app.exception_handler(RequestValidationError)
async def validation_error(
    request: Request,
    exc: RequestValidationError,
) -> JSONResponse:
    # TODO: preserve safe location/type signal without reflecting raw input.
    return JSONResponse(status_code=422, content={"error": "Invalid request"})


@app.post("/areas/{area_id}/expedientes")
def create_record(
    area_id: Annotated[int, Path(gt=0)],
    page: Annotated[int, Query(ge=1)],
    payload: RecordInput,
) -> dict[str, object]:
    return {"area_id": area_id, "page": page, "payload": payload.model_dump()}
