CREATE TABLE IF NOT EXISTS files (
    rel_path    TEXT PRIMARY KEY,
    size        INTEGER NOT NULL,
    mtime       INTEGER NOT NULL,
    sha256      TEXT NOT NULL,
    mime        TEXT NOT NULL DEFAULT '',
    ext         TEXT NOT NULL DEFAULT '',
    bucket      TEXT NOT NULL DEFAULT '',
    sub_bucket  TEXT NOT NULL DEFAULT '',
    indexed_at  INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_files_sha256 ON files(sha256);
CREATE INDEX IF NOT EXISTS idx_files_bucket ON files(bucket, sub_bucket);

CREATE VIRTUAL TABLE IF NOT EXISTS files_fts USING fts5(
    rel_path,
    content_text,
    content='',
    tokenize='unicode61 remove_diacritics 2'
);

CREATE TABLE IF NOT EXISTS csv_profiles (
    rel_path    TEXT PRIMARY KEY,
    cols_json   TEXT NOT NULL,
    n_rows      INTEGER NOT NULL,
    encoding    TEXT NOT NULL DEFAULT '',
    date_min    TEXT,
    date_max    TEXT,
    profiled_at INTEGER NOT NULL,
    FOREIGN KEY (rel_path) REFERENCES files(rel_path) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS pdf_extracts (
    rel_path     TEXT PRIMARY KEY,
    page_count   INTEGER NOT NULL,
    text_len     INTEGER NOT NULL,
    extracted_to TEXT,
    extracted_at INTEGER NOT NULL,
    FOREIGN KEY (rel_path) REFERENCES files(rel_path) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS knowledge_docs (
    rel_path         TEXT PRIMARY KEY,
    title            TEXT NOT NULL DEFAULT '',
    frontmatter_json TEXT NOT NULL DEFAULT '{}',
    headings_json    TEXT NOT NULL DEFAULT '[]',
    parsed_at        INTEGER NOT NULL,
    FOREIGN KEY (rel_path) REFERENCES files(rel_path) ON DELETE CASCADE
);
