"use strict";
/**
 * Version history and cron scheduling DB operations for yaml-workflows.
 *
 * Extracted from db.ts to keep each file under 300 lines.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createVersionSnapshot = createVersionSnapshot;
exports.getVersionHistory = getVersionHistory;
exports.getVersionSnapshot = getVersionSnapshot;
exports.markContentDeployed = markContentDeployed;
exports.markAppIdContentDeployed = markAppIdContentDeployed;
exports.updateCronSchedule = updateCronSchedule;
exports.clearCronSchedule = clearCronSchedule;
exports.getCronScheduledWorkflows = getCronScheduledWorkflows;
const db_1 = require("../../lib/db");
const defaults_1 = require("../../modules/defaults");
const sql_1 = require("./sql");
// -- Version history ---------------------------------------------------------
async function createVersionSnapshot(workflowId, version, yamlContent, activityManifest, inputSchema, outputSchema, inputFieldMeta, changeSummary) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.CREATE_VERSION_SNAPSHOT, [
        workflowId, version, yamlContent,
        JSON.stringify(activityManifest),
        JSON.stringify(inputSchema),
        JSON.stringify(outputSchema),
        JSON.stringify(inputFieldMeta || []),
        changeSummary || null,
    ]);
    return rows[0];
}
async function getVersionHistory(workflowId, limit = defaults_1.YAML_VERSION_LIMIT, offset = 0) {
    const pool = (0, db_1.getPool)();
    const [countResult, dataResult] = await Promise.all([
        pool.query(sql_1.COUNT_VERSIONS, [workflowId]),
        pool.query(sql_1.LIST_VERSIONS, [workflowId, limit, offset]),
    ]);
    return {
        versions: dataResult.rows,
        total: parseInt(countResult.rows[0].count, 10),
    };
}
async function getVersionSnapshot(workflowId, version) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.GET_VERSION_SNAPSHOT, [workflowId, version]);
    return rows[0] || null;
}
async function markContentDeployed(workflowId) {
    const pool = (0, db_1.getPool)();
    await pool.query(sql_1.MARK_CONTENT_DEPLOYED, [workflowId]);
}
async function markAppIdContentDeployed(appId) {
    const pool = (0, db_1.getPool)();
    await pool.query(sql_1.MARK_APP_ID_CONTENT_DEPLOYED, [appId]);
}
// -- Cron scheduling ---------------------------------------------------------
async function updateCronSchedule(id, cronSchedule, cronEnvelope, executeAs) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.UPDATE_CRON_SCHEDULE, [
        id,
        cronSchedule,
        cronEnvelope ? JSON.stringify(cronEnvelope) : null,
        executeAs,
    ]);
    return rows[0] || null;
}
async function clearCronSchedule(id) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.CLEAR_CRON_SCHEDULE, [id]);
    return rows[0] || null;
}
async function getCronScheduledWorkflows() {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.GET_CRON_SCHEDULED_WORKFLOWS);
    return rows;
}
