"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildAccountingAgentRouter = buildAccountingAgentRouter;
const express_1 = require("express");
const http_1 = require("../utils/http");
const PREFIX = "/api/accounting-agent/v1";
function buildAccountingAgentRouter(services) {
    const router = (0, express_1.Router)();
    const runtime = services.runtimeAdapter;
    router.post(`${PREFIX}/runs/start`, (req, res, next) => {
        try {
            const body = (req.body ?? {});
            const run = runtime.startRun({
                sessionId: body.sessionId ? String(body.sessionId) : undefined,
                initiator: body.initiator ? String(body.initiator) : "operator",
                source: body.source ? String(body.source) : "gui",
                metadata: (body.metadata ?? {}),
                idempotencyKey: body.idempotencyKey ? String(body.idempotencyKey) : undefined
            });
            (0, http_1.created)(res, {
                ok: true,
                sessionId: run.sessionId,
                runId: run.runId,
                status: run.status,
                run
            });
        }
        catch (error) {
            next(error);
        }
    });
    router.post(`${PREFIX}/runs/finish`, (req, res, next) => {
        try {
            const body = (req.body ?? {});
            const status = String(body.status ?? "DONE");
            if (!["DONE", "ERROR", "CANCELLED"].includes(status)) {
                throw new http_1.ApiError("INVALID_STATUS", `Invalid finish status: ${status}`, 400);
            }
            const run = runtime.finishRun({
                runId: String(body.runId ?? ""),
                status,
                source: body.source ? String(body.source) : "gui",
                reason: body.reason ? String(body.reason) : undefined,
                metadata: (body.metadata ?? {}),
                idempotencyKey: body.idempotencyKey ? String(body.idempotencyKey) : undefined
            });
            (0, http_1.ok)(res, {
                ok: true,
                run
            });
        }
        catch (error) {
            next(error);
        }
    });
    router.get(`${PREFIX}/runs`, (_req, res) => {
        (0, http_1.ok)(res, {
            ok: true,
            items: runtime.listRuns()
        });
    });
    router.get(`${PREFIX}/runs/:runId`, (req, res, next) => {
        try {
            const run = runtime.getRun(String(req.params.runId));
            if (!run) {
                throw new http_1.ApiError("RUN_NOT_FOUND", `Run not found: ${req.params.runId}`, 404);
            }
            (0, http_1.ok)(res, {
                ok: true,
                run
            });
        }
        catch (error) {
            next(error);
        }
    });
    router.post(`${PREFIX}/tasks/enqueue`, (req, res, next) => {
        try {
            const body = (req.body ?? {});
            const task = runtime.enqueueTask({
                runId: String(body.runId ?? ""),
                payload: (body.payload ?? {}),
                source: body.source ? String(body.source) : "gui",
                idempotencyKey: body.idempotencyKey ? String(body.idempotencyKey) : undefined
            });
            (0, http_1.created)(res, {
                ok: true,
                task
            });
        }
        catch (error) {
            next(error);
        }
    });
    router.post(`${PREFIX}/tasks/claim`, (_req, res) => {
        const task = runtime.claimTask();
        (0, http_1.ok)(res, {
            ok: true,
            task
        });
    });
    router.post(`${PREFIX}/tasks/:taskId/complete`, (req, res, next) => {
        try {
            const body = (req.body ?? {});
            const task = runtime.completeTask({
                taskId: String(req.params.taskId),
                result: (body.result ?? {}),
                source: body.source ? String(body.source) : "worker",
                idempotencyKey: body.idempotencyKey ? String(body.idempotencyKey) : undefined
            });
            (0, http_1.ok)(res, {
                ok: true,
                task
            });
        }
        catch (error) {
            next(error);
        }
    });
    router.post(`${PREFIX}/tasks/:taskId/error`, (req, res, next) => {
        try {
            const body = (req.body ?? {});
            const task = runtime.failTask({
                taskId: String(req.params.taskId),
                error: {
                    code: String(body.code ?? "TASK_ERROR"),
                    message: String(body.message ?? "Task failed"),
                    details: body.details
                },
                source: body.source ? String(body.source) : "worker",
                idempotencyKey: body.idempotencyKey ? String(body.idempotencyKey) : undefined
            });
            (0, http_1.ok)(res, {
                ok: true,
                task
            });
        }
        catch (error) {
            next(error);
        }
    });
    router.get(`${PREFIX}/results`, (_req, res) => {
        (0, http_1.ok)(res, {
            ok: true,
            items: runtime.getResults()
        });
    });
    router.get(`${PREFIX}/trace/run/:runId`, (req, res) => {
        (0, http_1.ok)(res, {
            ok: true,
            items: runtime.getRunTrace(String(req.params.runId))
        });
    });
    router.get(`${PREFIX}/health`, (_req, res) => {
        (0, http_1.ok)(res, runtime.health());
    });
    return router;
}
