-- FTS5 virtual table for full-text search on snippets.
-- This cannot be expressed in Drizzle ORM DDL; applied manually during DB init.

CREATE VIRTUAL TABLE IF NOT EXISTS snippets_fts USING fts5(
  content,
  title,
  breadcrumb,
  content='snippets',
  content_rowid='rowid',
  tokenize='porter unicode61'
);

-- Keep FTS index in sync with the snippets table via triggers.

CREATE TRIGGER IF NOT EXISTS snippets_ai AFTER INSERT ON snippets BEGIN
  INSERT INTO snippets_fts(rowid, content, title, breadcrumb)
  VALUES (new.rowid, new.content, new.title, new.breadcrumb);
END;

CREATE TRIGGER IF NOT EXISTS snippets_ad AFTER DELETE ON snippets BEGIN
  INSERT INTO snippets_fts(snippets_fts, rowid, content, title, breadcrumb)
  VALUES ('delete', old.rowid, old.content, old.title, old.breadcrumb);
END;

CREATE TRIGGER IF NOT EXISTS snippets_au AFTER UPDATE ON snippets BEGIN
  INSERT INTO snippets_fts(snippets_fts, rowid, content, title, breadcrumb)
  VALUES ('delete', old.rowid, old.content, old.title, old.breadcrumb);
  INSERT INTO snippets_fts(rowid, content, title, breadcrumb)
  VALUES (new.rowid, new.content, new.title, new.breadcrumb);
END;
