-- ==========================================
-- Switchboard Core — 003 Packages & Extensions (SQLite)
-- ==========================================

CREATE TABLE IF NOT EXISTS packages (
    id           TEXT PRIMARY KEY,
    title        TEXT NOT NULL,
    type         TEXT NOT NULL DEFAULT 'surface'
                 CHECK (type IN ('surface', 'extension', 'full', 'workflow', 'library')),
    version      TEXT NOT NULL DEFAULT '0.0.0',
    description  TEXT NOT NULL DEFAULT '',
    author       TEXT NOT NULL DEFAULT '',
    tier         TEXT NOT NULL DEFAULT 'browser'
                 CHECK (tier IN ('browser', 'starlark', 'sidecar')),
    is_system    INTEGER NOT NULL DEFAULT 0,
    scope        TEXT NOT NULL DEFAULT 'global'
                 CHECK (scope IN ('global', 'team', 'personal')),
    team_id      TEXT REFERENCES teams(id) ON DELETE CASCADE,
    installed_by TEXT REFERENCES users(id) ON DELETE SET NULL,
    manifest     TEXT NOT NULL DEFAULT '{}',
    enabled      INTEGER NOT NULL DEFAULT 1,
    status       TEXT NOT NULL DEFAULT 'active'
                 CHECK (status IN ('active', 'pending_review', 'suspended', 'dormant')),
    schema_version INTEGER NOT NULL DEFAULT 0,
    package_settings TEXT NOT NULL DEFAULT '{}',
    source       TEXT NOT NULL DEFAULT 'core'
                 CHECK (source IN ('core', 'builtin', 'extension', 'registry')),
    installed_at TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at   TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_packages_type ON packages(type);
CREATE INDEX IF NOT EXISTS idx_packages_enabled ON packages(enabled);
CREATE INDEX IF NOT EXISTS idx_packages_team ON packages(team_id);
CREATE INDEX IF NOT EXISTS idx_packages_source ON packages(source);
CREATE INDEX IF NOT EXISTS idx_packages_status ON packages(status);

CREATE TABLE IF NOT EXISTS package_user_settings (
    package_id   TEXT NOT NULL REFERENCES packages(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 (package_id, user_id)
);

CREATE TABLE IF NOT EXISTS extension_permissions (
    id          TEXT PRIMARY KEY,
    package_id  TEXT NOT NULL REFERENCES packages(id) ON DELETE CASCADE,
    permission  TEXT NOT NULL,
    granted     INTEGER NOT NULL DEFAULT 0,
    granted_by  TEXT REFERENCES users(id) ON DELETE SET NULL,
    granted_at  TEXT,
    created_at  TEXT NOT NULL DEFAULT (datetime('now')),
    UNIQUE(package_id, permission)
);

CREATE INDEX IF NOT EXISTS idx_ext_perm_package ON extension_permissions(package_id);
CREATE INDEX IF NOT EXISTS idx_ext_perm_granted ON extension_permissions(granted);

CREATE TABLE IF NOT EXISTS ext_data_tables (
    package_id  TEXT NOT NULL REFERENCES packages(id) ON DELETE CASCADE,
    table_name  TEXT NOT NULL,
    created_at  TEXT NOT NULL DEFAULT (datetime('now')),
    PRIMARY KEY (package_id, table_name)
);

CREATE INDEX IF NOT EXISTS idx_ext_data_tables_pkg ON ext_data_tables(package_id);

CREATE VIEW IF NOT EXISTS ext_view_users AS
    SELECT id, display_name, email FROM users;
