import { readFileSync, writeFileSync, existsSync, mkdirSync } from "fs";
import { join } from "path";
import { randomUUID } from "crypto";
import { DATA_DIR } from "../utils/paths.js";
const AGENTS_DIR = join(DATA_DIR, "agents");
function triggersPath(alias) {
    return join(AGENTS_DIR, alias, "triggers.json");
}
function ensureAgentDir(alias) {
    const dir = join(AGENTS_DIR, alias);
    if (!existsSync(dir)) {
        mkdirSync(dir, { recursive: true });
    }
}
function readTriggers(alias) {
    const path = triggersPath(alias);
    if (!existsSync(path))
        return [];
    try {
        const data = readFileSync(path, "utf8");
        return JSON.parse(data);
    }
    catch {
        return [];
    }
}
function writeTriggers(alias, triggers) {
    ensureAgentDir(alias);
    writeFileSync(triggersPath(alias), JSON.stringify(triggers, null, 2));
}
export function listTriggers(alias) {
    return readTriggers(alias);
}
export function getTrigger(alias, triggerId) {
    const triggers = readTriggers(alias);
    return triggers.find((t) => t.id === triggerId);
}
export function createTrigger(alias, trigger) {
    const triggers = readTriggers(alias);
    const newTrigger = {
        ...trigger,
        id: randomUUID(),
    };
    triggers.push(newTrigger);
    writeTriggers(alias, triggers);
    return newTrigger;
}
export function updateTrigger(alias, triggerId, updates) {
    const triggers = readTriggers(alias);
    const index = triggers.findIndex((t) => t.id === triggerId);
    if (index === -1)
        return undefined;
    // Don't allow changing the id
    const { id: _id, ...safeUpdates } = updates;
    triggers[index] = { ...triggers[index], ...safeUpdates };
    writeTriggers(alias, triggers);
    return triggers[index];
}
export function deleteTrigger(alias, triggerId) {
    const triggers = readTriggers(alias);
    const index = triggers.findIndex((t) => t.id === triggerId);
    if (index === -1)
        return false;
    triggers.splice(index, 1);
    writeTriggers(alias, triggers);
    return true;
}
