"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTask = createTask;
exports.updateTask = updateTask;
exports.appendMilestones = appendMilestones;
exports.getTask = getTask;
exports.getTaskBySignalId = getTaskBySignalId;
exports.getTaskByWorkflowId = getTaskByWorkflowId;
exports.listTasks = listTasks;
const db_1 = require("../../lib/db");
const sql_1 = require("./sql");
async function createTask(input) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.CREATE_TASK, [
        input.workflow_id,
        input.workflow_type,
        input.lt_type,
        input.task_queue || null,
        input.signal_id,
        input.parent_workflow_id,
        input.origin_id || null,
        input.parent_id || null,
        input.envelope,
        input.metadata ? JSON.stringify(input.metadata) : null,
        input.priority || 2,
        input.trace_id || null,
        input.span_id || null,
        input.initiated_by || null,
        input.principal_type || null,
        input.executing_as || null,
        input.status || null,
    ]);
    return rows[0];
}
async function updateTask(id, input) {
    const pool = (0, db_1.getPool)();
    const sets = [];
    const values = [];
    let idx = 1;
    if (input.status !== undefined) {
        sets.push(`status = $${idx++}`);
        values.push(input.status);
    }
    if (input.completed_at !== undefined) {
        sets.push(`completed_at = $${idx++}`);
        values.push(input.completed_at);
    }
    if (input.error !== undefined) {
        sets.push(`error = $${idx++}`);
        values.push(input.error);
    }
    if (input.data !== undefined) {
        sets.push(`data = $${idx++}`);
        values.push(input.data);
    }
    if (input.milestones !== undefined) {
        sets.push(`milestones = $${idx++}`);
        values.push(JSON.stringify(input.milestones));
    }
    values.push(id);
    const { rows } = await pool.query(`UPDATE lt_tasks SET ${sets.join(', ')} WHERE id = $${idx} RETURNING *`, values);
    return rows[0];
}
async function appendMilestones(id, milestones) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.APPEND_MILESTONES, [JSON.stringify(milestones), id]);
    return rows[0];
}
async function getTask(id) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.GET_TASK, [id]);
    return rows[0] || null;
}
async function getTaskBySignalId(signalId) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.GET_TASK_BY_SIGNAL_ID, [signalId]);
    return rows[0] || null;
}
async function getTaskByWorkflowId(workflowId) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.GET_TASK_BY_WORKFLOW_ID, [workflowId]);
    return rows[0] || null;
}
async function listTasks(filters) {
    const pool = (0, db_1.getPool)();
    const conditions = [];
    const values = [];
    let idx = 1;
    if (filters.status) {
        conditions.push(`status = $${idx++}`);
        values.push(filters.status);
    }
    if (filters.lt_type) {
        conditions.push(`lt_type = $${idx++}`);
        values.push(filters.lt_type);
    }
    if (filters.workflow_type) {
        conditions.push(`workflow_type = $${idx++}`);
        values.push(filters.workflow_type);
    }
    if (filters.workflow_id) {
        conditions.push(`workflow_id = $${idx++}`);
        values.push(filters.workflow_id);
    }
    if (filters.parent_workflow_id) {
        conditions.push(`parent_workflow_id = $${idx++}`);
        values.push(filters.parent_workflow_id);
    }
    if (filters.origin_id) {
        conditions.push(`origin_id = $${idx++}`);
        values.push(filters.origin_id);
    }
    const where = conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
    const limit = filters.limit || 50;
    const offset = filters.offset || 0;
    const [countResult, dataResult] = await Promise.all([
        pool.query(`SELECT COUNT(*) FROM lt_tasks ${where}`, values),
        pool.query(`SELECT * FROM lt_tasks ${where} ORDER BY created_at DESC LIMIT $${idx++} OFFSET $${idx++}`, [...values, limit, offset]),
    ]);
    return {
        tasks: dataResult.rows,
        total: parseInt(countResult.rows[0].count, 10),
    };
}
