-- Chat Switchboard — 012 Extensions (SQLite)

CREATE TABLE IF NOT EXISTS extensions (
    id          TEXT PRIMARY KEY,
    ext_id      TEXT NOT NULL UNIQUE,
    name        TEXT NOT NULL,
    version     TEXT NOT NULL DEFAULT '0.0.0',
    tier        TEXT NOT NULL DEFAULT 'browser',
    description TEXT NOT NULL DEFAULT '',
    author      TEXT NOT NULL DEFAULT '',
    manifest    TEXT NOT NULL DEFAULT '{}',
    is_system   INTEGER NOT NULL DEFAULT 0,
    is_enabled  INTEGER NOT NULL DEFAULT 1,
    scope       TEXT NOT NULL DEFAULT 'global',
    team_id     TEXT REFERENCES teams(id) ON DELETE CASCADE,
    installed_by TEXT REFERENCES users(id) ON DELETE SET NULL,
    created_at  TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at  TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_extensions_tier ON extensions(tier);
CREATE INDEX IF NOT EXISTS idx_extensions_enabled ON extensions(is_enabled);

CREATE TABLE IF NOT EXISTS extension_user_settings (
    extension_id TEXT NOT NULL REFERENCES extensions(id) ON DELETE CASCADE,
    user_id      TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    settings     TEXT NOT NULL DEFAULT '{}',
    is_enabled   INTEGER NOT NULL DEFAULT 1,
    PRIMARY KEY (extension_id, user_id)
);
