-- Chat Switchboard — 004 Personas & Grants (SQLite)

CREATE TABLE IF NOT EXISTS personas (
    id                  TEXT PRIMARY KEY,
    name                TEXT NOT NULL,
    handle              TEXT,
    description         TEXT DEFAULT '',
    icon                TEXT DEFAULT '',
    avatar              TEXT DEFAULT '',
    base_model_id       TEXT NOT NULL,
    provider_config_id  TEXT REFERENCES provider_configs(id) ON DELETE SET NULL,
    system_prompt       TEXT DEFAULT '',
    temperature         REAL,
    max_tokens          INTEGER,
    thinking_budget     INTEGER,
    top_p               REAL,
    memory_enabled      INTEGER NOT NULL DEFAULT 1,
    memory_extraction_prompt TEXT,
    scope               TEXT NOT NULL CHECK (scope IN ('global', 'team', 'personal')),
    owner_id            TEXT,
    created_by          TEXT NOT NULL REFERENCES users(id),
    is_active           INTEGER DEFAULT 1,
    is_shared           INTEGER DEFAULT 0,
    created_at          TEXT DEFAULT (datetime('now')),
    updated_at          TEXT DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_personas_scope ON personas(scope);
CREATE INDEX IF NOT EXISTS idx_personas_owner ON personas(owner_id);
CREATE INDEX IF NOT EXISTS idx_personas_active ON personas(is_active);
CREATE UNIQUE INDEX IF NOT EXISTS idx_personas_handle ON personas(handle) WHERE handle IS NOT NULL;

CREATE TABLE IF NOT EXISTS persona_groups (
    id          TEXT PRIMARY KEY,
    name        TEXT NOT NULL,
    description TEXT DEFAULT '',
    owner_id    TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    scope       TEXT NOT NULL DEFAULT 'personal' CHECK (scope IN ('global', 'team', 'personal')),
    team_id     TEXT REFERENCES teams(id) ON DELETE CASCADE,
    created_at  TEXT DEFAULT (datetime('now')),
    updated_at  TEXT DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_persona_groups_owner ON persona_groups(owner_id);

CREATE TABLE IF NOT EXISTS persona_group_members (
    id          TEXT PRIMARY KEY,
    group_id    TEXT NOT NULL REFERENCES persona_groups(id) ON DELETE CASCADE,
    persona_id  TEXT NOT NULL REFERENCES personas(id) ON DELETE CASCADE,
    is_leader   INTEGER NOT NULL DEFAULT 0,
    sort_order  INTEGER NOT NULL DEFAULT 0,
    UNIQUE(group_id, persona_id)
);

CREATE INDEX IF NOT EXISTS idx_persona_group_members_group ON persona_group_members(group_id);

CREATE TABLE IF NOT EXISTS persona_grants (
    id              TEXT PRIMARY KEY,
    persona_id      TEXT NOT NULL REFERENCES personas(id) ON DELETE CASCADE,
    grant_type      TEXT NOT NULL,
    grant_ref       TEXT NOT NULL,
    config          TEXT DEFAULT '{}',
    created_at      TEXT DEFAULT (datetime('now')),
    UNIQUE(persona_id, grant_type, grant_ref)
);

CREATE INDEX IF NOT EXISTS idx_persona_grants_persona ON persona_grants(persona_id);
CREATE INDEX IF NOT EXISTS idx_persona_grants_type ON persona_grants(grant_type);

CREATE TABLE IF NOT EXISTS resource_grants (
    id              TEXT PRIMARY KEY,
    resource_type   TEXT NOT NULL CHECK (resource_type IN ('persona', 'knowledge_base', 'project')),
    resource_id     TEXT NOT NULL,
    grant_scope     TEXT NOT NULL DEFAULT 'team_only'
                    CHECK (grant_scope IN ('team_only', 'global', 'groups')),
    granted_groups  TEXT NOT NULL DEFAULT '[]',
    created_by      TEXT NOT NULL REFERENCES users(id),
    created_at      TEXT DEFAULT (datetime('now')),
    updated_at      TEXT DEFAULT (datetime('now')),
    UNIQUE(resource_type, resource_id)
);

CREATE INDEX IF NOT EXISTS idx_resource_grants_resource ON resource_grants(resource_type, resource_id);
