/**
 * SQLite index schema for the workspace index.db.
 * Tables are created with IF NOT EXISTS so this is safe to run on every load.
 */
export const SCHEMA_SQL = `
CREATE TABLE IF NOT EXISTS pages (
    path TEXT PRIMARY KEY,
    title TEXT NOT NULL DEFAULT '',
    modified_at INTEGER NOT NULL,
    word_count INTEGER NOT NULL DEFAULT 0,
    content_hash TEXT NOT NULL DEFAULT ''
);

CREATE TABLE IF NOT EXISTS blocks (
    id TEXT PRIMARY KEY,
    page_path TEXT NOT NULL REFERENCES pages(path) ON DELETE CASCADE,
    content TEXT NOT NULL DEFAULT '',
    depth INTEGER NOT NULL DEFAULT 0,
    block_type TEXT CHECK(block_type IN (
        'paragraph', 'heading', 'task', 'callout',
        'blockquote', 'code', 'table', 'math', 'property'
    )),
    has_id_stamp INTEGER NOT NULL DEFAULT 0,
    content_hash TEXT NOT NULL DEFAULT '',
    embedding BLOB,
    embedding_hash TEXT NOT NULL DEFAULT '',
    modified_at INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS links (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    source_block_id TEXT NOT NULL REFERENCES blocks(id) ON DELETE CASCADE,
    source_page_path TEXT NOT NULL REFERENCES pages(path) ON DELETE CASCADE,
    target TEXT NOT NULL,
    link_type TEXT NOT NULL CHECK(link_type IN ('page-ref', 'block-ref', 'tag')),
    offset_in_block INTEGER NOT NULL DEFAULT 0
);

CREATE VIRTUAL TABLE IF NOT EXISTS blocks_fts USING fts5(
    content,
    block_id UNINDEXED,
    page_path UNINDEXED,
    content='blocks',
    content_rowid='rowid'
);

CREATE TRIGGER IF NOT EXISTS blocks_ai AFTER INSERT ON blocks BEGIN
    INSERT INTO blocks_fts(rowid, content, block_id, page_path)
    VALUES (new.rowid, new.content, new.id, new.page_path);
END;

CREATE TRIGGER IF NOT EXISTS blocks_ad AFTER DELETE ON blocks BEGIN
    INSERT INTO blocks_fts(blocks_fts, rowid, content, block_id, page_path)
    VALUES ('delete', old.rowid, old.content, old.id, old.page_path);
END;

CREATE TRIGGER IF NOT EXISTS blocks_au AFTER UPDATE ON blocks BEGIN
    INSERT INTO blocks_fts(blocks_fts, rowid, content, block_id, page_path)
    VALUES ('delete', old.rowid, old.content, old.id, old.page_path);
    INSERT INTO blocks_fts(rowid, content, block_id, page_path)
    VALUES (new.rowid, new.content, new.id, new.page_path);
END;

CREATE TABLE IF NOT EXISTS properties (
    block_id TEXT NOT NULL REFERENCES blocks(id) ON DELETE CASCADE,
    page_path TEXT NOT NULL REFERENCES pages(path) ON DELETE CASCADE,
    key TEXT NOT NULL,
    value TEXT NOT NULL,
    PRIMARY KEY (block_id, key)
);

CREATE INDEX IF NOT EXISTS idx_properties_key ON properties(key, value);
CREATE INDEX IF NOT EXISTS idx_properties_page ON properties(page_path, key);
CREATE INDEX IF NOT EXISTS idx_properties_block ON properties(block_id);

CREATE INDEX IF NOT EXISTS idx_blocks_page ON blocks(page_path, depth);
CREATE INDEX IF NOT EXISTS idx_links_target ON links(target, link_type);
CREATE INDEX IF NOT EXISTS idx_links_source ON links(source_block_id);
CREATE INDEX IF NOT EXISTS idx_links_tag ON links(target) WHERE link_type = 'tag';
`
