CREATE TABLE IF NOT EXISTS file_index_parent_path (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  path TEXT NOT NULL UNIQUE,
  path_hash TEXT NOT NULL,
  updated_at TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS file_index_child_path (
  parent_id INTEGER NOT NULL,
  path TEXT NOT NULL,
  PRIMARY KEY (parent_id, path),
  FOREIGN KEY (parent_id) REFERENCES file_index_parent_path(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_file_index_child_path_parent ON file_index_child_path(parent_id);

CREATE TABLE IF NOT EXISTS symbol_index_parent_path (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  path TEXT NOT NULL UNIQUE,
  path_hash TEXT NOT NULL,
  updated_at TEXT NOT NULL,
  index_version INTEGER NOT NULL DEFAULT 1
);

CREATE TABLE IF NOT EXISTS symbol_index_tag (
  parent_id INTEGER NOT NULL,
  file_path TEXT NOT NULL,
  name TEXT NOT NULL,
  role TEXT NOT NULL,
  kind TEXT NOT NULL,
  start_line INTEGER NOT NULL,
  start_column INTEGER NOT NULL,
  end_line INTEGER NOT NULL,
  end_column INTEGER NOT NULL,
  scope_start_line INTEGER,
  scope_start_column INTEGER,
  nav_target_path TEXT,
  parent_scope_start_line INTEGER,
  parent_scope_start_column INTEGER,
  member_parent_scope_start_line INTEGER,
  member_parent_scope_start_column INTEGER,
  scope_chain TEXT,
  PRIMARY KEY (parent_id, file_path, start_line, start_column, role, kind, name),
  FOREIGN KEY (parent_id) REFERENCES symbol_index_parent_path(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_symbol_index_tag_parent_name ON symbol_index_tag(parent_id, name);
CREATE INDEX IF NOT EXISTS idx_symbol_index_tag_parent_file ON symbol_index_tag(parent_id, file_path);
CREATE INDEX IF NOT EXISTS idx_symbol_index_tag_parent_role ON symbol_index_tag(parent_id, role);
CREATE INDEX IF NOT EXISTS idx_symbol_index_tag_parent_role_file_name
  ON symbol_index_tag(parent_id, role, file_path, name);

CREATE TABLE IF NOT EXISTS symbol_index_import (
  parent_id INTEGER NOT NULL,
  file_path TEXT NOT NULL,
  local_name TEXT NOT NULL,
  source_name TEXT NOT NULL,
  module_specifier TEXT NOT NULL,
  source_file_path TEXT,
  import_kind TEXT NOT NULL,
  start_line INTEGER NOT NULL,
  start_column INTEGER NOT NULL,
  PRIMARY KEY (parent_id, file_path, local_name, start_line, start_column),
  FOREIGN KEY (parent_id) REFERENCES symbol_index_parent_path(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_symbol_index_import_parent_file
  ON symbol_index_import(parent_id, file_path);

CREATE INDEX IF NOT EXISTS idx_symbol_index_import_local_name
  ON symbol_index_import(parent_id, file_path, local_name);

CREATE TABLE IF NOT EXISTS symbol_index_scope (
  parent_id INTEGER NOT NULL,
  file_path TEXT NOT NULL,
  start_line INTEGER NOT NULL,
  start_column INTEGER NOT NULL,
  parent_start_line INTEGER,
  parent_start_column INTEGER,
  PRIMARY KEY (parent_id, file_path, start_line, start_column),
  FOREIGN KEY (parent_id) REFERENCES symbol_index_parent_path(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_symbol_index_scope_parent_file
  ON symbol_index_scope(parent_id, file_path);
