/**
 * Work Lifecycle Hook Storage
 *
 * CRUD operations for hook configurations in the workspace database.
 * Hooks are stored in the pmo_work_hooks table.
 */
import { randomUUID } from 'node:crypto';
/** Table name for work hooks. */
export const HOOKS_TABLE = 'pmo_work_hooks';
/** SQL to create the hooks table. */
export const HOOKS_TABLE_SCHEMA = `
  CREATE TABLE IF NOT EXISTS ${HOOKS_TABLE} (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL UNIQUE,
    event TEXT NOT NULL,
    action_type TEXT NOT NULL CHECK (action_type IN ('shell', 'webhook', 'log')),
    action_value TEXT NOT NULL,
    enabled INTEGER NOT NULL DEFAULT 1,
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  )
`;
export const HOOKS_TABLE_INDEX = `
  CREATE INDEX IF NOT EXISTS idx_pmo_work_hooks_event ON ${HOOKS_TABLE}(event);
  CREATE INDEX IF NOT EXISTS idx_pmo_work_hooks_enabled ON ${HOOKS_TABLE}(enabled);
`;
/**
 * Ensure the hooks table exists in the database.
 * Safe to call multiple times.
 */
export function ensureHooksTable(db) {
    db.exec(HOOKS_TABLE_SCHEMA);
    db.exec(HOOKS_TABLE_INDEX);
}
/**
 * Convert a database row to a WorkHookConfig.
 */
function rowToConfig(row) {
    return {
        id: row.id,
        name: row.name,
        event: row.event,
        actionType: row.action_type,
        actionValue: row.action_value,
        enabled: row.enabled === 1,
        description: row.description,
        createdAt: row.created_at,
    };
}
/**
 * WorkHookStorage provides CRUD operations for hook configurations.
 */
export class WorkHookStorage {
    db;
    constructor(db) {
        this.db = db;
        ensureHooksTable(db);
    }
    /**
     * List all hooks, optionally filtered by event name or enabled status.
     */
    list(options) {
        let sql = `SELECT * FROM ${HOOKS_TABLE} WHERE 1=1`;
        const params = [];
        if (options?.event) {
            sql += ' AND event = ?';
            params.push(options.event);
        }
        if (options?.enabled !== undefined) {
            sql += ' AND enabled = ?';
            params.push(options.enabled ? 1 : 0);
        }
        sql += ' ORDER BY created_at ASC';
        const rows = this.db.prepare(sql).all(...params);
        return rows.map(rowToConfig);
    }
    /**
     * Get a single hook by ID.
     */
    getById(id) {
        const row = this.db.prepare(`SELECT * FROM ${HOOKS_TABLE} WHERE id = ?`).get(id);
        return row ? rowToConfig(row) : null;
    }
    /**
     * Get a single hook by name.
     */
    getByName(name) {
        const row = this.db.prepare(`SELECT * FROM ${HOOKS_TABLE} WHERE name = ?`).get(name);
        return row ? rowToConfig(row) : null;
    }
    /**
     * Create a new hook configuration.
     */
    create(params) {
        const id = randomUUID();
        this.db.prepare(`
      INSERT INTO ${HOOKS_TABLE} (id, name, event, action_type, action_value, description)
      VALUES (?, ?, ?, ?, ?, ?)
    `).run(id, params.name, params.event, params.actionType, params.actionValue, params.description ?? null);
        return this.getById(id);
    }
    /**
     * Delete a hook by ID.
     */
    delete(id) {
        const result = this.db.prepare(`DELETE FROM ${HOOKS_TABLE} WHERE id = ?`).run(id);
        return result.changes > 0;
    }
    /**
     * Enable or disable a hook.
     */
    setEnabled(id, enabled) {
        const result = this.db.prepare(`UPDATE ${HOOKS_TABLE} SET enabled = ? WHERE id = ?`).run(enabled ? 1 : 0, id);
        return result.changes > 0;
    }
}
//# sourceMappingURL=storage.js.map