"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 });
exports.createTask = createTask;
exports.listTasks = listTasks;
exports.getProcessStats = getProcessStats;
exports.listProcesses = listProcesses;
exports.getProcess = getProcess;
exports.getTask = getTask;
const taskService = __importStar(require("../services/task"));
const escalationService = __importStar(require("../services/escalation"));
/**
 * Create a task record.
 *
 * Tasks represent workflow executions tracked by the LT interceptor.
 * Required fields: `workflow_id`, `workflow_type`, `lt_type`,
 * `signal_id`, `parent_workflow_id`, and `envelope`.
 *
 * @returns `{ status: 201, data: <task record> }`
 */
async function createTask(input, auth) {
    try {
        const { workflow_id, workflow_type, lt_type, signal_id, parent_workflow_id } = input;
        if (!workflow_id || typeof workflow_id !== 'string') {
            return { status: 400, error: 'workflow_id is required' };
        }
        if (!workflow_type || typeof workflow_type !== 'string') {
            return { status: 400, error: 'workflow_type is required' };
        }
        if (!lt_type || typeof lt_type !== 'string') {
            return { status: 400, error: 'lt_type is required' };
        }
        if (!signal_id || typeof signal_id !== 'string') {
            return { status: 400, error: 'signal_id is required' };
        }
        if (!parent_workflow_id || typeof parent_workflow_id !== 'string') {
            return { status: 400, error: 'parent_workflow_id is required' };
        }
        const task = await taskService.createTask({
            workflow_id,
            workflow_type,
            lt_type,
            task_queue: input.task_queue,
            signal_id,
            parent_workflow_id,
            origin_id: input.origin_id,
            parent_id: input.parent_id,
            envelope: input.envelope ?? '{}',
            metadata: input.metadata,
            priority: input.priority,
            trace_id: input.trace_id,
            span_id: input.span_id,
        });
        return { status: 201, data: task };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
/**
 * List tasks with optional filters.
 *
 * Tasks represent workflow executions tracked by the LT interceptor.
 *
 * @param input.status — filter by `pending` or `completed`
 * @param input.lt_type — filter by interceptor classification
 * @param input.workflow_type — filter by workflow function name
 * @param input.workflow_id — filter by HotMesh workflow ID
 * @param input.parent_workflow_id — filter by parent orchestrator ID
 * @param input.origin_id — filter by root process origin ID
 * @param input.limit — max results (default: 50)
 * @param input.offset — pagination offset (default: 0)
 * @returns `{ status: 200, data: { tasks, total } }`
 */
async function listTasks(input) {
    try {
        const result = await taskService.listTasks(input);
        return { status: 200, data: result };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
/**
 * Return aggregate process statistics.
 *
 * @param input.period — time window (`1h`, `24h`, `7d`, `30d`)
 * @returns `{ status: 200, data: <process stats> }`
 */
async function getProcessStats(input) {
    try {
        const stats = await taskService.getProcessStats(input.period);
        return { status: 200, data: stats };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
/**
 * List processes (grouped by origin_id) with optional filters.
 *
 * @param input.limit — max results (default: 50)
 * @param input.offset — pagination offset
 * @param input.workflow_type — filter by workflow type
 * @param input.status — filter by status
 * @param input.search — full-text search across process fields
 * @returns `{ status: 200, data: { processes, total } }`
 */
async function listProcesses(input) {
    try {
        const result = await taskService.listProcesses(input);
        return { status: 200, data: result };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
/**
 * Get a single process with all its tasks and escalations.
 *
 * @param input.originId — root process origin ID
 * @returns `{ status: 200, data: { origin_id, tasks, escalations } }`
 */
async function getProcess(input) {
    try {
        const [tasks, escalations] = await Promise.all([
            taskService.getProcessTasks(input.originId),
            escalationService.getEscalationsByOriginId(input.originId),
        ]);
        return {
            status: 200,
            data: { origin_id: input.originId, tasks, escalations },
        };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
/**
 * Get a single task by ID.
 *
 * @param input.id — task UUID
 * @returns `{ status: 200, data: <task record> }` or 404
 */
async function getTask(input) {
    try {
        const task = await taskService.getTask(input.id);
        if (!task) {
            return { status: 404, error: 'Task not found' };
        }
        return { status: 200, data: task };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
