"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const api = __importStar(require("../api/tasks"));
const router = (0, express_1.Router)();
/**
 * POST /api/tasks
 * Create a new task record.
 * Body: { workflow_id, workflow_type, lt_type, signal_id, parent_workflow_id, envelope, ... }
 */
router.post('/', async (req, res) => {
    const result = await api.createTask({
        workflow_id: req.body?.workflow_id,
        workflow_type: req.body?.workflow_type,
        lt_type: req.body?.lt_type,
        task_queue: req.body?.task_queue,
        signal_id: req.body?.signal_id,
        parent_workflow_id: req.body?.parent_workflow_id,
        origin_id: req.body?.origin_id,
        parent_id: req.body?.parent_id,
        envelope: req.body?.envelope,
        metadata: req.body?.metadata,
        priority: req.body?.priority,
        trace_id: req.body?.trace_id,
        span_id: req.body?.span_id,
    }, { userId: req.auth?.userId ?? '', role: req.auth?.role, scopes: req.auth?.scopes });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/tasks
 * List tasks with optional filters.
 * Query: ?status=completed&workflow_type=reviewContent&workflow_id=abc&lt_type=...&origin_id=...&limit=50&offset=0
 */
router.get('/', async (req, res) => {
    const result = await api.listTasks({
        status: req.query.status,
        lt_type: req.query.lt_type,
        workflow_type: req.query.workflow_type,
        workflow_id: req.query.workflow_id,
        parent_workflow_id: req.query.parent_workflow_id,
        origin_id: req.query.origin_id,
        limit: req.query.limit ? parseInt(req.query.limit, 10) : undefined,
        offset: req.query.offset ? parseInt(req.query.offset, 10) : undefined,
    });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/tasks/processes/stats
 * Aggregated process statistics with optional time period.
 */
router.get('/processes/stats', async (req, res) => {
    const result = await api.getProcessStats({
        period: req.query.period || undefined,
    });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/tasks/processes
 * List distinct origin_id values with summary stats.
 */
router.get('/processes', async (req, res) => {
    const result = await api.listProcesses({
        limit: req.query.limit ? parseInt(req.query.limit, 10) : undefined,
        offset: req.query.offset ? parseInt(req.query.offset, 10) : undefined,
        workflow_type: req.query.workflow_type || undefined,
        status: req.query.status || undefined,
        search: req.query.search || undefined,
    });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/tasks/processes/:originId
 * Get all tasks and escalations for a process (origin_id).
 */
router.get('/processes/:originId', async (req, res) => {
    const result = await api.getProcess({ originId: req.params.originId });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/tasks/:id
 * Get a single task by ID.
 */
router.get('/:id', async (req, res) => {
    const result = await api.getTask({ id: req.params.id });
    res.status(result.status).json(result.data ?? { error: result.error });
});
exports.default = router;
