import { Hono } from "hono";
import type { AppBindings } from "../../core/utils";
import {
    isErrPayload,
    jsonError,
    parseJsonBody,
    toStatusCode,
} from "../../core/utils";
import { type SessionPrepareInput, sessionPrepareSchema } from "./data";
import { prepareSession } from "./service";

export const orchestrateRouter = new Hono<AppBindings>().post(
    "/sessions/prepare",
    async (c) => {
        const fctx = c.get("fctx");
        const body = await parseJsonBody<SessionPrepareInput, typeof sessionPrepareSchema>(
            c.req.raw,
            sessionPrepareSchema,
            fctx,
        );

        if (isErrPayload(body)) {
            const response = jsonError(body);
            return c.json(response.body, toStatusCode(response.status));
        }

        const response = await prepareSession(fctx, body);
        return c.json(response.body, toStatusCode(response.status));
    },
);
