-- pagepiper initial schema
-- Run via: conda run -n cf python scripts/db_migrate.py

CREATE TABLE IF NOT EXISTS documents (
    id          TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),
    title       TEXT NOT NULL,
    file_path   TEXT NOT NULL UNIQUE,
    status      TEXT NOT NULL DEFAULT 'pending',
    -- status: pending | processing | ready | error
    task_id     TEXT,
    page_count  INTEGER,
    error_msg   TEXT,
    created_at  TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at  TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS page_chunks (
    id          TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),
    doc_id      TEXT NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
    page_number INTEGER NOT NULL,
    text        TEXT NOT NULL DEFAULT '',
    source      TEXT NOT NULL,
    -- source: text_layer | ocr
    word_count  INTEGER NOT NULL DEFAULT 0,
    created_at  TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_page_chunks_doc_id ON page_chunks(doc_id);
CREATE INDEX IF NOT EXISTS idx_page_chunks_doc_page ON page_chunks(doc_id, page_number);
