"""
Roster AI — API HTTP (FastAPI) autour du solveur OR-Tools CP-SAT.

Endpoints :
  GET  /health        → liveness
  POST /solve         → résout un horaire (payload = voir README/sample_request.json)

Pensé pour tourner à côté du hub (targo-hub l'appelle), pas exposé publiquement.
"""
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from typing import Any
import os

from solver import solve_roster

app = FastAPI(title="Roster AI Solver", version="0.1.0")

API_TOKEN = os.environ.get("ROSTER_SOLVER_TOKEN", "")


class SolveRequest(BaseModel):
    horizon: dict
    shift_templates: list
    technicians: list
    coverage: list = []
    weights: dict | None = None
    max_seconds: float | None = None


@app.get("/health")
def health():
    return {"ok": True, "service": "roster-solver"}


@app.post("/solve")
def solve(req: SolveRequest):
    try:
        result = solve_roster(req.model_dump())
        return result
    except Exception as e:  # noqa: BLE001 — renvoyer l'erreur proprement au hub
        return JSONResponse(status_code=400, content={"status": "ERROR", "message": str(e)})
