CREATE TABLE IF NOT EXISTS `search_document` (
  `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
  `source_kind` text NOT NULL,
  `source_ref` text NOT NULL,
  `scope` text NOT NULL,
  `user_id` text,
  `ticker` text,
  `accession_number` text,
  `title` text,
  `content_text` text NOT NULL,
  `content_hash` text NOT NULL,
  `metadata` text,
  `index_status` text NOT NULL DEFAULT 'pending',
  `indexed_at` text,
  `last_error` text,
  `created_at` text NOT NULL,
  `updated_at` text NOT NULL,
  FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);

CREATE TABLE IF NOT EXISTS `search_chunk` (
  `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
  `document_id` integer NOT NULL,
  `chunk_index` integer NOT NULL,
  `chunk_text` text NOT NULL,
  `char_count` integer NOT NULL,
  `start_offset` integer NOT NULL,
  `end_offset` integer NOT NULL,
  `heading_path` text,
  `citation_label` text NOT NULL,
  `created_at` text NOT NULL,
  FOREIGN KEY (`document_id`) REFERENCES `search_document`(`id`) ON UPDATE no action ON DELETE cascade
);

CREATE UNIQUE INDEX IF NOT EXISTS `search_document_source_uidx`
  ON `search_document` (`scope`, ifnull(`user_id`, ''), `source_kind`, `source_ref`);
CREATE INDEX IF NOT EXISTS `search_document_scope_idx`
  ON `search_document` (`scope`, `source_kind`, `ticker`, `updated_at`);
CREATE INDEX IF NOT EXISTS `search_document_accession_idx`
  ON `search_document` (`accession_number`, `source_kind`);

CREATE UNIQUE INDEX IF NOT EXISTS `search_chunk_document_chunk_uidx`
  ON `search_chunk` (`document_id`, `chunk_index`);
CREATE INDEX IF NOT EXISTS `search_chunk_document_idx`
  ON `search_chunk` (`document_id`);
