"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listSubscriptions = listSubscriptions;
exports.getSubscription = getSubscription;
exports.createSubscription = createSubscription;
exports.updateSubscription = updateSubscription;
exports.deleteSubscription = deleteSubscription;
exports.listActiveSubscriptions = listActiveSubscriptions;
exports.seedSubscription = seedSubscription;
const db_1 = require("../../lib/db");
const subscription_sql_1 = require("./subscription-sql");
async function listSubscriptions(agentId) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(subscription_sql_1.LIST_SUBSCRIPTIONS, [agentId]);
    return rows;
}
async function getSubscription(id) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(subscription_sql_1.GET_SUBSCRIPTION, [id]);
    return rows[0] ?? null;
}
async function createSubscription(agentId, data) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(subscription_sql_1.INSERT_SUBSCRIPTION, [
        agentId,
        data.topic,
        data.filter ? JSON.stringify(data.filter) : null,
        data.reaction_type,
        data.workflow_type ?? null,
        data.pipeline_id ?? null,
        data.mcp_prompt ?? null,
        JSON.stringify(data.input_mapping ?? {}),
        data.execute_as ?? null,
        data.enabled !== false,
        data.server_id ?? null,
        data.tool_name ?? null,
    ]);
    return rows[0];
}
async function updateSubscription(id, data) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(subscription_sql_1.UPDATE_SUBSCRIPTION, [
        id,
        data.topic ?? null,
        data.filter ? JSON.stringify(data.filter) : null,
        data.reaction_type ?? null,
        data.workflow_type ?? null,
        data.pipeline_id ?? null,
        data.mcp_prompt ?? null,
        data.input_mapping ? JSON.stringify(data.input_mapping) : null,
        data.execute_as ?? null,
        data.enabled ?? null,
        data.server_id ?? null,
        data.tool_name ?? null,
    ]);
    return rows[0] ?? null;
}
async function deleteSubscription(id) {
    const pool = (0, db_1.getPool)();
    const { rowCount } = await pool.query(subscription_sql_1.DELETE_SUBSCRIPTION, [id]);
    return (rowCount ?? 0) > 0;
}
async function listActiveSubscriptions() {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(subscription_sql_1.LIST_ACTIVE_SUBSCRIPTIONS);
    return rows;
}
/**
 * Seed a subscription at startup (insert-if-absent).
 * Conflict on (agent_id, topic) — DB is source of truth after first boot.
 */
async function seedSubscription(agentId, data) {
    const pool = (0, db_1.getPool)();
    const { rowCount } = await pool.query(subscription_sql_1.SEED_SUBSCRIPTION, [
        agentId,
        data.topic,
        data.filter ? JSON.stringify(data.filter) : null,
        data.reaction_type,
        data.workflow_type ?? null,
        data.pipeline_id ?? null,
        data.mcp_prompt ?? null,
        JSON.stringify(data.input_mapping ?? {}),
        data.execute_as ?? null,
        data.server_id ?? null,
        data.tool_name ?? null,
    ]);
    return (rowCount ?? 0) > 0;
}
