-- ==========================================
-- Switchboard Core — 011 Triggers & Scheduled Tasks (SQLite)
-- ==========================================

-- ── Extension Triggers (event + webhook) ──────

CREATE TABLE IF NOT EXISTS triggers (
    id            TEXT PRIMARY KEY,
    package_id    TEXT NOT NULL REFERENCES packages(id) ON DELETE CASCADE,
    type          TEXT NOT NULL CHECK (type IN ('webhook', 'event')),
    enabled       INTEGER NOT NULL DEFAULT 1,

    -- Webhook triggers
    slug          TEXT,
    secret        TEXT,

    -- Event triggers
    event_pattern TEXT,

    -- Common
    entry_point   TEXT NOT NULL,
    config        TEXT NOT NULL DEFAULT '{}',
    fire_count    INTEGER NOT NULL DEFAULT 0,
    last_fire_at  TEXT,
    last_error    TEXT,
    created_at    TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at    TEXT NOT NULL DEFAULT (datetime('now')),

    UNIQUE(package_id, type, slug),
    UNIQUE(package_id, type, event_pattern)
);

CREATE INDEX IF NOT EXISTS idx_triggers_package ON triggers(package_id);
CREATE INDEX IF NOT EXISTS idx_triggers_type ON triggers(type);
CREATE INDEX IF NOT EXISTS idx_triggers_enabled ON triggers(enabled);
CREATE INDEX IF NOT EXISTS idx_triggers_webhook ON triggers(package_id, slug);

-- ── Scheduled Tasks (user-created cron) ───────

CREATE TABLE IF NOT EXISTS scheduled_tasks (
    id              TEXT PRIMARY KEY,
    name            TEXT NOT NULL,
    description     TEXT NOT NULL DEFAULT '',
    creator_id      TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    run_as          TEXT NOT NULL DEFAULT 'creator'
                    CHECK (run_as IN ('creator', 'system')),
    cron_expr       TEXT NOT NULL,
    next_fire_at    TEXT,
    last_fire_at    TEXT,
    enabled         INTEGER NOT NULL DEFAULT 1,
    script          TEXT NOT NULL DEFAULT '',
    template_id     TEXT,
    template_params TEXT NOT NULL DEFAULT '{}',
    fire_count      INTEGER NOT NULL DEFAULT 0,
    last_error      TEXT,
    last_duration_ms INTEGER,
    created_at      TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at      TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_sched_tasks_creator ON scheduled_tasks(creator_id);
CREATE INDEX IF NOT EXISTS idx_sched_tasks_enabled ON scheduled_tasks(enabled);
CREATE INDEX IF NOT EXISTS idx_sched_tasks_next_fire ON scheduled_tasks(next_fire_at);

-- ── Trigger Logs (unified execution log) ──────

CREATE TABLE IF NOT EXISTS trigger_logs (
    id                TEXT PRIMARY KEY,
    trigger_id        TEXT REFERENCES triggers(id) ON DELETE CASCADE,
    scheduled_task_id TEXT REFERENCES scheduled_tasks(id) ON DELETE CASCADE,
    fired_at          TEXT NOT NULL DEFAULT (datetime('now')),
    duration_ms       INTEGER,
    success           INTEGER NOT NULL DEFAULT 1,
    error             TEXT,
    output            TEXT
);

CREATE INDEX IF NOT EXISTS idx_trigger_logs_trigger ON trigger_logs(trigger_id);
CREATE INDEX IF NOT EXISTS idx_trigger_logs_sched ON trigger_logs(scheduled_task_id);
CREATE INDEX IF NOT EXISTS idx_trigger_logs_fired ON trigger_logs(fired_at);
