"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createWorkflowSet = createWorkflowSet;
exports.getWorkflowSet = getWorkflowSet;
exports.updateWorkflowSetPlan = updateWorkflowSetPlan;
exports.appendWorkflowSetSpecification = appendWorkflowSetSpecification;
exports.updateWorkflowSetStatus = updateWorkflowSetStatus;
exports.updateWorkflowSetSourceWorkflow = updateWorkflowSetSourceWorkflow;
exports.deleteWorkflowSet = deleteWorkflowSet;
exports.listWorkflowSets = listWorkflowSets;
const db_1 = require("../../lib/db");
const sql_1 = require("./sql");
const DEFAULT_LIMIT = 20;
async function createWorkflowSet(input) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.CREATE_WORKFLOW_SET, [
        input.name,
        input.description || null,
        input.specification,
        JSON.stringify(input.plan || []),
        input.namespaces || [],
        input.source_workflow_id || null,
    ]);
    return rows[0];
}
async function getWorkflowSet(id) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.GET_WORKFLOW_SET, [id]);
    return rows[0] || null;
}
async function updateWorkflowSetPlan(id, plan, namespaces) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.UPDATE_WORKFLOW_SET_PLAN, [
        id,
        JSON.stringify(plan),
        namespaces,
    ]);
    return rows[0] || null;
}
async function appendWorkflowSetSpecification(id, specification) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.APPEND_WORKFLOW_SET_SPECIFICATION, [id, specification]);
    return rows[0] || null;
}
async function updateWorkflowSetStatus(id, status) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.UPDATE_WORKFLOW_SET_STATUS, [id, status]);
    return rows[0] || null;
}
async function updateWorkflowSetSourceWorkflow(id, sourceWorkflowId) {
    const pool = (0, db_1.getPool)();
    await pool.query('UPDATE lt_workflow_sets SET source_workflow_id = $2 WHERE id = $1', [id, sourceWorkflowId]);
}
async function deleteWorkflowSet(id) {
    const pool = (0, db_1.getPool)();
    const { rowCount } = await pool.query(sql_1.DELETE_WORKFLOW_SET, [id]);
    return (rowCount ?? 0) > 0;
}
async function listWorkflowSets(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.search) {
        conditions.push(`(name ILIKE $${idx} OR description ILIKE $${idx})`);
        values.push(`%${filters.search}%`);
        idx++;
    }
    const where = conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
    const limit = filters.limit || DEFAULT_LIMIT;
    const offset = filters.offset || 0;
    const [countResult, dataResult] = await Promise.all([
        pool.query(`SELECT COUNT(*) FROM lt_workflow_sets ${where}`, values),
        pool.query(`${sql_1.LIST_WORKFLOW_SETS_BASE} ${where} ORDER BY created_at DESC LIMIT $${idx++} OFFSET $${idx++}`, [...values, limit, offset]),
    ]);
    return {
        sets: dataResult.rows,
        total: parseInt(countResult.rows[0].count, 10),
    };
}
