-- Individual tag/import rows keyed by content hash (no JSON blobs).

DROP TABLE IF EXISTS symbol_blob_tag_index;
DROP TABLE IF EXISTS symbol_file_blob;

CREATE TABLE symbol_parent_file_map_new (
  parent_id INTEGER NOT NULL,
  file_path TEXT NOT NULL,
  content_hash TEXT,
  PRIMARY KEY (parent_id, file_path),
  FOREIGN KEY (parent_id) REFERENCES symbol_index_parent_path(id) ON DELETE CASCADE
);

INSERT INTO symbol_parent_file_map_new (parent_id, file_path, content_hash)
SELECT parent_id, file_path, content_hash FROM symbol_parent_file_map;

DROP TABLE symbol_parent_file_map;
ALTER TABLE symbol_parent_file_map_new RENAME TO symbol_parent_file_map;

CREATE INDEX IF NOT EXISTS idx_symbol_parent_file_map_parent ON symbol_parent_file_map(parent_id);
CREATE INDEX IF NOT EXISTS idx_symbol_parent_file_map_hash ON symbol_parent_file_map(content_hash);

CREATE TABLE IF NOT EXISTS symbol_index_tag (
  content_hash TEXT 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,
  parent_scope_start_line INTEGER,
  parent_scope_start_column INTEGER,
  nav_target_path TEXT,
  scope_chain TEXT,
  PRIMARY KEY (content_hash, file_path, start_line, start_column, role, kind, name)
);

CREATE INDEX IF NOT EXISTS idx_symbol_index_tag_hash_name ON symbol_index_tag(content_hash, name);
CREATE INDEX IF NOT EXISTS idx_symbol_index_tag_hash_file ON symbol_index_tag(content_hash, file_path);
CREATE INDEX IF NOT EXISTS idx_symbol_index_tag_hash_role_file_name
  ON symbol_index_tag(content_hash, role, file_path, name);

CREATE TABLE IF NOT EXISTS symbol_index_import (
  content_hash TEXT 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 (content_hash, file_path, local_name, start_line, start_column)
);

CREATE INDEX IF NOT EXISTS idx_symbol_index_import_hash_file ON symbol_index_import(content_hash, file_path);
CREATE INDEX IF NOT EXISTS idx_symbol_index_import_hash_local ON symbol_index_import(content_hash, file_path, local_name);
