-- KB spine: document_chunk — chunk-level embeddings, 1:N against envelope
-- Additive: does not modify 001_envelope.sql. envelope stays untouched.
-- Version: 002 — chunks (bge-m3 dense embeddings, dim=1024)

CREATE TABLE IF NOT EXISTS document_chunk (
    id           BIGSERIAL   PRIMARY KEY,
    envelope_id  TEXT        NOT NULL REFERENCES envelope(id) ON DELETE CASCADE,
    chunk_index  INT         NOT NULL,          -- order within envelope, 0-based
    text         TEXT        NOT NULL,
    embedding    VECTOR(1024),                  -- bge-m3 dense; NULL until embedded
    model        TEXT        NOT NULL,          -- e.g. 'bge-m3' — tracks re-index on model change
    created_at   TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE (envelope_id, chunk_index)
);

CREATE INDEX IF NOT EXISTS document_chunk_envelope_idx ON document_chunk (envelope_id);
CREATE INDEX IF NOT EXISTS document_chunk_embedding_hnsw_idx
    ON document_chunk USING hnsw (embedding vector_cosine_ops);
