-- 002_fts_indexes.sql
-- FTS5 virtual table for player typeahead search
-- Synced with players table via triggers

CREATE VIRTUAL TABLE IF NOT EXISTS players_fts USING fts5(
    name, nickname, email,
    content='players',
    content_rowid='rowid'
);

-- Sync triggers: keep FTS index up to date with players table

CREATE TRIGGER IF NOT EXISTS players_ai AFTER INSERT ON players BEGIN
    INSERT INTO players_fts(rowid, name, nickname, email)
    VALUES (new.rowid, new.name, new.nickname, new.email);
END;

CREATE TRIGGER IF NOT EXISTS players_ad AFTER DELETE ON players BEGIN
    INSERT INTO players_fts(players_fts, rowid, name, nickname, email)
    VALUES ('delete', old.rowid, old.name, old.nickname, old.email);
END;

CREATE TRIGGER IF NOT EXISTS players_au AFTER UPDATE ON players BEGIN
    INSERT INTO players_fts(players_fts, rowid, name, nickname, email)
    VALUES ('delete', old.rowid, old.name, old.nickname, old.email);
    INSERT INTO players_fts(rowid, name, nickname, email)
    VALUES (new.rowid, new.name, new.nickname, new.email);
END;
