"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildAssistantRouter = buildAssistantRouter;
const express_1 = require("express");
const http_1 = require("../utils/http");
function buildAssistantRouter(services) {
    const router = (0, express_1.Router)();
    router.post("/api/assistant/message", async (req, res, next) => {
        try {
            const payload = (req.body ?? {});
            const userMessageSource = typeof payload.user_message === "string"
                ? payload.user_message
                : typeof payload.message === "string"
                    ? payload.message
                    : "";
            const userMessage = userMessageSource.trim();
            if (!userMessage) {
                throw new http_1.ApiError("INVALID_ASSISTANT_MESSAGE", "Field `user_message` or `message` is required.", 400);
            }
            const response = await services.assistantService.handleMessage({
                ...payload,
                user_message: userMessage,
                message: userMessage,
                mode: typeof payload.mode === "string" ? payload.mode : "assistant"
            });
            (0, http_1.ok)(res, response);
        }
        catch (error) {
            next(error);
        }
    });
    router.get("/api/assistant/session/:session_id", (req, res, next) => {
        try {
            const sessionId = String(req.params.session_id ?? "");
            const session = services.assistantService.getSession(sessionId);
            if (!session) {
                throw new http_1.ApiError("ASSISTANT_SESSION_NOT_FOUND", `Session not found: ${sessionId}`, 404);
            }
            (0, http_1.ok)(res, {
                ok: true,
                session
            });
        }
        catch (error) {
            next(error);
        }
    });
    return router;
}
