/**
 * Database Schema Definitions
 *
 * SQLite schema for code-buddy persistent storage including:
 * - Memories with vector embeddings
 * - Session history with costs
 * - Code embeddings for semantic search
 * - Tool statistics and learning
 * - Analytics and cost tracking
 */
// ============================================================================
// Schema Version
// ============================================================================
export const SCHEMA_VERSION = 1;
// ============================================================================
// Table Definitions
// ============================================================================
export const SCHEMA_SQL = `
-- Schema version tracking
CREATE TABLE IF NOT EXISTS schema_version (
  version INTEGER PRIMARY KEY,
  applied_at TEXT DEFAULT CURRENT_TIMESTAMP
);

-- ============================================================================
-- MEMORIES TABLE
-- Stores persistent memories with embeddings for semantic search
-- ============================================================================
CREATE TABLE IF NOT EXISTS memories (
  id TEXT PRIMARY KEY,
  type TEXT NOT NULL CHECK(type IN ('fact', 'preference', 'pattern', 'decision', 'context', 'summary', 'instruction', 'error', 'definition', 'convention')),
  scope TEXT NOT NULL CHECK(scope IN ('user', 'project')),
  project_id TEXT,
  content TEXT NOT NULL,
  embedding BLOB,  -- Float32Array as binary
  importance REAL DEFAULT 0.5 CHECK(importance >= 0 AND importance <= 1),
  access_count INTEGER DEFAULT 0,
  created_at TEXT DEFAULT CURRENT_TIMESTAMP,
  last_accessed TEXT DEFAULT CURRENT_TIMESTAMP,
  expires_at TEXT,
  metadata TEXT,  -- JSON for additional data
  UNIQUE(scope, project_id, content)
);

CREATE INDEX IF NOT EXISTS idx_memories_type ON memories(type);
CREATE INDEX IF NOT EXISTS idx_memories_scope ON memories(scope, project_id);
CREATE INDEX IF NOT EXISTS idx_memories_importance ON memories(importance DESC);
CREATE INDEX IF NOT EXISTS idx_memories_last_accessed ON memories(last_accessed DESC);

-- ============================================================================
-- SESSIONS TABLE
-- Stores conversation sessions with full history
-- ============================================================================
CREATE TABLE IF NOT EXISTS sessions (
  id TEXT PRIMARY KEY,
  project_id TEXT,
  project_path TEXT,
  name TEXT,
  model TEXT,
  created_at TEXT DEFAULT CURRENT_TIMESTAMP,
  updated_at TEXT DEFAULT CURRENT_TIMESTAMP,
  total_tokens_in INTEGER DEFAULT 0,
  total_tokens_out INTEGER DEFAULT 0,
  total_cost REAL DEFAULT 0,
  message_count INTEGER DEFAULT 0,
  tool_calls_count INTEGER DEFAULT 0,
  is_archived INTEGER DEFAULT 0,
  metadata TEXT  -- JSON
);

CREATE INDEX IF NOT EXISTS idx_sessions_project ON sessions(project_id);
CREATE INDEX IF NOT EXISTS idx_sessions_updated ON sessions(updated_at DESC);

-- ============================================================================
-- MESSAGES TABLE
-- Individual messages within sessions
-- ============================================================================
CREATE TABLE IF NOT EXISTS messages (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  session_id TEXT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
  role TEXT NOT NULL CHECK(role IN ('user', 'assistant', 'system', 'tool')),
  content TEXT,
  tool_calls TEXT,  -- JSON array of tool calls
  tool_call_id TEXT,
  tokens INTEGER DEFAULT 0,
  created_at TEXT DEFAULT CURRENT_TIMESTAMP,
  metadata TEXT  -- JSON
);

CREATE INDEX IF NOT EXISTS idx_messages_session ON messages(session_id);

-- ============================================================================
-- CODE EMBEDDINGS TABLE
-- Vector embeddings for code chunks
-- ============================================================================
CREATE TABLE IF NOT EXISTS code_embeddings (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  project_id TEXT NOT NULL,
  file_path TEXT NOT NULL,
  chunk_index INTEGER NOT NULL,
  chunk_text TEXT NOT NULL,
  chunk_hash TEXT NOT NULL,
  embedding BLOB NOT NULL,  -- Float32Array as binary
  symbol_type TEXT,  -- function, class, interface, etc.
  symbol_name TEXT,
  start_line INTEGER,
  end_line INTEGER,
  language TEXT,
  created_at TEXT DEFAULT CURRENT_TIMESTAMP,
  updated_at TEXT DEFAULT CURRENT_TIMESTAMP,
  UNIQUE(project_id, file_path, chunk_index)
);

CREATE INDEX IF NOT EXISTS idx_code_embeddings_project ON code_embeddings(project_id);
CREATE INDEX IF NOT EXISTS idx_code_embeddings_file ON code_embeddings(project_id, file_path);
CREATE INDEX IF NOT EXISTS idx_code_embeddings_symbol ON code_embeddings(symbol_type, symbol_name);

-- ============================================================================
-- TOOL STATISTICS TABLE
-- Tracks tool usage, success rates, and performance
-- ============================================================================
CREATE TABLE IF NOT EXISTS tool_stats (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  tool_name TEXT NOT NULL,
  project_id TEXT,
  success_count INTEGER DEFAULT 0,
  failure_count INTEGER DEFAULT 0,
  total_time_ms INTEGER DEFAULT 0,
  avg_time_ms REAL GENERATED ALWAYS AS (
    CASE WHEN (success_count + failure_count) > 0
    THEN total_time_ms * 1.0 / (success_count + failure_count)
    ELSE 0 END
  ) STORED,
  cache_hits INTEGER DEFAULT 0,
  cache_misses INTEGER DEFAULT 0,
  last_used TEXT DEFAULT CURRENT_TIMESTAMP,
  updated_at TEXT DEFAULT CURRENT_TIMESTAMP,
  UNIQUE(tool_name, project_id)
);

CREATE INDEX IF NOT EXISTS idx_tool_stats_name ON tool_stats(tool_name);

-- ============================================================================
-- REPAIR LEARNING TABLE
-- Stores what repair strategies work for which error patterns
-- ============================================================================
CREATE TABLE IF NOT EXISTS repair_learning (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  error_pattern TEXT NOT NULL,  -- Normalized error message pattern
  error_type TEXT NOT NULL CHECK(error_type IN ('compile', 'runtime', 'test', 'lint', 'type')),
  strategy TEXT NOT NULL,
  context_hash TEXT,  -- Hash of surrounding code context
  language TEXT,
  framework TEXT,
  success_count INTEGER DEFAULT 0,
  failure_count INTEGER DEFAULT 0,
  success_rate REAL GENERATED ALWAYS AS (
    CASE WHEN (success_count + failure_count) > 0
    THEN success_count * 1.0 / (success_count + failure_count)
    ELSE 0 END
  ) STORED,
  avg_attempts REAL DEFAULT 1,
  last_used TEXT DEFAULT CURRENT_TIMESTAMP,
  created_at TEXT DEFAULT CURRENT_TIMESTAMP,
  examples TEXT,  -- JSON array of successful fix examples
  UNIQUE(error_pattern, strategy, context_hash)
);

CREATE INDEX IF NOT EXISTS idx_repair_learning_pattern ON repair_learning(error_pattern);
CREATE INDEX IF NOT EXISTS idx_repair_learning_success ON repair_learning(success_rate DESC);

-- ============================================================================
-- ANALYTICS TABLE
-- Daily aggregated usage statistics
-- ============================================================================
CREATE TABLE IF NOT EXISTS analytics (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  date TEXT NOT NULL,
  project_id TEXT,
  model TEXT,
  tokens_in INTEGER DEFAULT 0,
  tokens_out INTEGER DEFAULT 0,
  cost REAL DEFAULT 0,
  requests INTEGER DEFAULT 0,
  tool_calls INTEGER DEFAULT 0,
  errors INTEGER DEFAULT 0,
  avg_response_time_ms REAL DEFAULT 0,
  cache_hit_rate REAL DEFAULT 0,
  session_count INTEGER DEFAULT 0,
  UNIQUE(date, project_id, model)
);

CREATE INDEX IF NOT EXISTS idx_analytics_date ON analytics(date DESC);
CREATE INDEX IF NOT EXISTS idx_analytics_project ON analytics(project_id, date DESC);

-- ============================================================================
-- CONVENTIONS TABLE
-- Learned coding conventions per project
-- ============================================================================
CREATE TABLE IF NOT EXISTS conventions (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  project_id TEXT NOT NULL,
  category TEXT NOT NULL,  -- naming, structure, style, testing, docs
  pattern TEXT NOT NULL,
  description TEXT,
  confidence REAL DEFAULT 0.5 CHECK(confidence >= 0 AND confidence <= 1),
  occurrences INTEGER DEFAULT 1,
  examples TEXT,  -- JSON array of examples
  created_at TEXT DEFAULT CURRENT_TIMESTAMP,
  updated_at TEXT DEFAULT CURRENT_TIMESTAMP,
  UNIQUE(project_id, category, pattern)
);

CREATE INDEX IF NOT EXISTS idx_conventions_project ON conventions(project_id);
CREATE INDEX IF NOT EXISTS idx_conventions_confidence ON conventions(confidence DESC);

-- ============================================================================
-- CHECKPOINTS TABLE
-- File checkpoints for undo/restore
-- ============================================================================
CREATE TABLE IF NOT EXISTS checkpoints (
  id TEXT PRIMARY KEY,
  project_id TEXT NOT NULL,
  description TEXT,
  created_at TEXT DEFAULT CURRENT_TIMESTAMP,
  file_count INTEGER DEFAULT 0,
  total_size INTEGER DEFAULT 0,
  metadata TEXT  -- JSON
);

CREATE INDEX IF NOT EXISTS idx_checkpoints_project ON checkpoints(project_id, created_at DESC);

-- ============================================================================
-- CHECKPOINT FILES TABLE
-- Individual file snapshots
-- ============================================================================
CREATE TABLE IF NOT EXISTS checkpoint_files (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  checkpoint_id TEXT NOT NULL REFERENCES checkpoints(id) ON DELETE CASCADE,
  file_path TEXT NOT NULL,
  content TEXT,
  content_hash TEXT,
  existed INTEGER DEFAULT 1,
  UNIQUE(checkpoint_id, file_path)
);

CREATE INDEX IF NOT EXISTS idx_checkpoint_files_checkpoint ON checkpoint_files(checkpoint_id);

-- ============================================================================
-- CACHE TABLE
-- General-purpose cache with TTL
-- ============================================================================
CREATE TABLE IF NOT EXISTS cache (
  key TEXT PRIMARY KEY,
  value TEXT NOT NULL,
  embedding BLOB,  -- Optional embedding for semantic cache
  created_at TEXT DEFAULT CURRENT_TIMESTAMP,
  expires_at TEXT,
  hits INTEGER DEFAULT 0,
  category TEXT  -- api, tool, search, etc.
);

CREATE INDEX IF NOT EXISTS idx_cache_expires ON cache(expires_at);
CREATE INDEX IF NOT EXISTS idx_cache_category ON cache(category);
`;
// ============================================================================
// Migration Scripts
// ============================================================================
export const MIGRATIONS = {
    1: SCHEMA_SQL,
    // Future migrations will be added here
    // 2: `ALTER TABLE memories ADD COLUMN new_field TEXT;`,
};
//# sourceMappingURL=schema.js.map