# ai_mouse/server/routes_verify.py
"""Verification route: generate trajectories."""
from __future__ import annotations

import logging

from fastapi import APIRouter, HTTPException
from pydantic import BaseModel

logger = logging.getLogger(__name__)

router = APIRouter()

# ---------------------------------------------------------------------------
# Request models
# ---------------------------------------------------------------------------


class VerifyRequest(BaseModel):
    start: list[int]
    end: list[int]
    n_paths: int = 5
    model_dir: str | None = None


# ---------------------------------------------------------------------------
# Routes
# ---------------------------------------------------------------------------


@router.post("/verify")
def verify(req: VerifyRequest) -> dict:
    # Uses the bundled ONNX model exposed via the public ai_mouse API.
    # The legacy req.model_dir / data/models_v2 .pt path is no longer wired
    # in; export a fresh .onnx via `python -m tools.export_onnx` to update.
    from ai_mouse import generate

    n = max(1, min(req.n_paths, 12))
    start = tuple(req.start)  # type: ignore[arg-type]
    end = tuple(req.end)  # type: ignore[arg-type]

    paths = []
    try:
        for _ in range(n):
            pts = generate(start=start, end=end)
            paths.append([[x, y, t] for x, y, t in pts])
    except FileNotFoundError as exc:
        raise HTTPException(status_code=404, detail=str(exc)) from exc

    return {"paths": paths}
