"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseVersionFromYaml = parseVersionFromYaml;
exports.updateYamlWorkflowStatus = updateYamlWorkflowStatus;
exports.listYamlWorkflows = listYamlWorkflows;
exports.findYamlWorkflowsByTags = findYamlWorkflowsByTags;
const db_1 = require("../../lib/db");
const defaults_1 = require("../../modules/defaults");
const sql_1 = require("./sql");
/**
 * Extract the `app.version` value from YAML content using a simple regex.
 * Returns null if not found.
 */
function parseVersionFromYaml(yaml) {
    const match = yaml.match(/^app:\s*\n(?:.*\n)*?\s+version:\s*['"]?(\S+?)['"]?\s*$/m);
    if (match)
        return match[1];
    const lines = yaml.split('\n');
    let inApp = false;
    for (const line of lines) {
        if (/^app:\s*$/.test(line)) {
            inApp = true;
            continue;
        }
        if (inApp && /^\S/.test(line))
            break;
        if (inApp) {
            const vm = line.match(/^\s+version:\s*['"]?(.+?)['"]?\s*$/);
            if (vm)
                return vm[1];
        }
    }
    return null;
}
async function updateYamlWorkflowStatus(id, status) {
    const pool = (0, db_1.getPool)();
    const timestampField = status === 'deployed' ? ', deployed_at = NOW()' :
        status === 'active' ? ', activated_at = NOW()' : '';
    const { rows } = await pool.query(`${sql_1.UPDATE_STATUS_BASE}${timestampField}${sql_1.UPDATE_STATUS_SUFFIX}`, [id, status]);
    return rows[0] || null;
}
async function listYamlWorkflows(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.graph_topic) {
        conditions.push(`graph_topic = $${idx++}`);
        values.push(filters.graph_topic);
    }
    if (filters.app_id) {
        conditions.push(`app_id = $${idx++}`);
        values.push(filters.app_id);
    }
    if (filters.tags?.length) {
        conditions.push(`tags && $${idx++}::text[]`);
        values.push(filters.tags);
    }
    if (filters.search) {
        conditions.push(`(name ILIKE $${idx} OR graph_topic ILIKE $${idx} OR description ILIKE $${idx} OR app_id ILIKE $${idx})`);
        values.push(`%${filters.search}%`);
        idx++;
    }
    if (filters.source_workflow_id) {
        conditions.push(`source_workflow_id = $${idx++}`);
        values.push(filters.source_workflow_id);
    }
    if (filters.set_id) {
        conditions.push(`set_id = $${idx++}`);
        values.push(filters.set_id);
    }
    const where = conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
    const limit = filters.limit || defaults_1.YAML_LIST_LIMIT;
    const offset = filters.offset || 0;
    const [countResult, dataResult] = await Promise.all([
        pool.query(`SELECT COUNT(*) FROM lt_yaml_workflows ${where}`, values),
        pool.query(`SELECT * FROM lt_yaml_workflows ${where} ORDER BY created_at DESC LIMIT $${idx++} OFFSET $${idx++}`, [...values, limit, offset]),
    ]);
    return {
        workflows: dataResult.rows,
        total: parseInt(countResult.rows[0].count, 10),
    };
}
/**
 * Find active YAML workflows matching any of the given tags.
 * Uses GIN index on tags column for efficient lookup.
 */
async function findYamlWorkflowsByTags(tags, match = 'any') {
    const pool = (0, db_1.getPool)();
    const sql = match === 'all' ? sql_1.FIND_BY_TAGS_ALL : sql_1.FIND_BY_TAGS_ANY;
    const { rows } = await pool.query(sql, [tags]);
    return rows;
}
