#pragma once
#include <string_view>

namespace file_indexer {

inline constexpr int SCHEMA_VERSION = 1;

inline constexpr std::string_view INIT_SQL = R"sql(
CREATE TABLE IF NOT EXISTS scan_history (
		id INTEGER PRIMARY KEY AUTOINCREMENT,
		status INTEGER NOT NULL,
		created_at INT DEFAULT (unixepoch()),
		finished_at INT, -- null while the scan is running
		entrypoint TEXT NOT NULL,
		error TEXT,
		type INT NOT NULL, -- full/incremental
		indexed_file_count INT DEFAULT 0
);

-- serves the per-directory cutoff lookups issued by every incremental scan
CREATE INDEX IF NOT EXISTS scan_history_entrypoint_idx
	ON scan_history(entrypoint, status, created_at);

CREATE TABLE IF NOT EXISTS indexed_file (
	id INTEGER PRIMARY KEY AUTOINCREMENT,
	path TEXT UNIQUE NOT NULL,
	skeleton_path TEXT NOT NULL,
	parent_id INT,
	last_modified_at INT,
	indexed_at INT NOT NULL DEFAULT (unixepoch()),
	type INT NOT NULL DEFAULT 0, -- 0 file, 1 directory
	category INT NOT NULL DEFAULT 0,
	size_bytes INT,
	mime_type_id INT
);

CREATE TABLE IF NOT EXISTS mime_type (
	id INTEGER PRIMARY KEY AUTOINCREMENT,
	name TEXT UNIQUE NOT NULL
);

CREATE INDEX IF NOT EXISTS indexed_file_parent_id_idx ON indexed_file(parent_id);

-- serves the recently-active-directories query backing the dynamic watch set
CREATE INDEX IF NOT EXISTS indexed_file_dir_mtime_idx
	ON indexed_file(last_modified_at DESC) WHERE type = 1;

CREATE INDEX IF NOT EXISTS indexed_file_category_idx ON indexed_file(category);
CREATE INDEX IF NOT EXISTS indexed_file_mime_type_idx ON indexed_file(mime_type_id);

CREATE VIRTUAL TABLE IF NOT EXISTS path_idx USING fts5(
	path, content=indexed_file, tokenize='fuzzy_trigram remove_diacritics 2'
);

CREATE TRIGGER IF NOT EXISTS path_idx_ai AFTER INSERT ON indexed_file BEGIN
  INSERT INTO path_idx(rowid, path) VALUES (new.id, new.path);END;

CREATE TRIGGER IF NOT EXISTS path_idx_ad AFTER DELETE ON indexed_file BEGIN
  INSERT INTO path_idx(path_idx, rowid, path) VALUES('delete', old.id, old.path);END;

-- trigram index over compact per-component abbreviation skeletons. Documents are
-- generated by C++ as skeletonized path tokens ("vicinae/settings.json" ->
-- "vcn stngs jsn") while the tokenizer skeletonizes queries and adds skip-grams.
CREATE VIRTUAL TABLE IF NOT EXISTS skeleton_idx USING fts5(
	skeleton_path, content=indexed_file, tokenize='fuzzy_trigram remove_diacritics 2 skeleton 1 skipgrams 1'
);

CREATE TRIGGER IF NOT EXISTS skeleton_idx_ai AFTER INSERT ON indexed_file BEGIN
  INSERT INTO skeleton_idx(rowid, skeleton_path) VALUES (new.id, new.skeleton_path);END;

CREATE TRIGGER IF NOT EXISTS skeleton_idx_au AFTER UPDATE ON indexed_file BEGIN
  INSERT INTO skeleton_idx(skeleton_idx, rowid, skeleton_path) VALUES('delete', old.id, old.skeleton_path);
  INSERT INTO skeleton_idx(rowid, skeleton_path) VALUES (new.id, new.skeleton_path);END;

CREATE TRIGGER IF NOT EXISTS skeleton_idx_ad AFTER DELETE ON indexed_file BEGIN
  INSERT INTO skeleton_idx(skeleton_idx, rowid, skeleton_path) VALUES('delete', old.id, old.skeleton_path);END;

-- spellfix1 typo-correction vocabulary, fully rebuilt from indexed_file basenames
-- after successful scans. Derived data: no schema version bump needed.
CREATE VIRTUAL TABLE IF NOT EXISTS spellfix_vocab USING spellfix1;
)sql";

}; // namespace file_indexer
