-- compose/init/03_rag_tracking.sql
-- RAG tracking tables for indexing status and analytics

-- Track which posts are indexed in Qdrant
CREATE TABLE IF NOT EXISTS post_vectors (
  id            INT AUTO_INCREMENT PRIMARY KEY,
  post_id       INT NOT NULL,
  collection_name VARCHAR(100) NOT NULL,
  vector_ids    JSON NOT NULL COMMENT 'Array of Qdrant point IDs for this post',
  provider      VARCHAR(50) NOT NULL COMMENT 'Provider used for embeddings (openai, openrouter, claude)',
  model         VARCHAR(100) NOT NULL COMMENT 'Specific model used for embeddings',
  chunk_count   INT NOT NULL COMMENT 'Number of chunks/vectors created',
  indexed_at    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  file_hash     VARCHAR(64) NOT NULL COMMENT 'MD5 hash of post content for change detection',

  FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE,
  UNIQUE KEY uniq_post_collection (post_id, collection_name),
  INDEX idx_collection (collection_name),
  INDEX idx_provider (provider),
  INDEX idx_indexed_at (indexed_at)
) ENGINE=InnoDB COMMENT='Tracks RAG indexing status for posts';

-- Track RAG query history for analytics
CREATE TABLE IF NOT EXISTS rag_queries (
  id            INT AUTO_INCREMENT PRIMARY KEY,
  user_id       INT NOT NULL,
  query_text    TEXT NOT NULL,
  provider      VARCHAR(50) NOT NULL COMMENT 'Provider used for search/completion',
  model         VARCHAR(100) NULL COMMENT 'Specific model used',
  results_count INT NOT NULL COMMENT 'Number of results returned',
  locale        VARCHAR(8) NULL COMMENT 'Language/locale of the query',
  created_at    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
  INDEX idx_user (user_id),
  INDEX idx_created (created_at),
  INDEX idx_provider (provider)
) ENGINE=InnoDB COMMENT='Analytics for RAG queries';
