CREATE TABLE transcription_blocks (
    id            UUID             PRIMARY KEY DEFAULT gen_random_uuid(),
    annotation_id UUID             NOT NULL REFERENCES document_annotations(id) ON DELETE RESTRICT,
    document_id   UUID             NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
    text          TEXT             NOT NULL DEFAULT '' CHECK (length(text) <= 10000),
    label         VARCHAR(200),
    sort_order    INTEGER          NOT NULL DEFAULT 0,
    version       INTEGER          NOT NULL DEFAULT 0,
    created_by    UUID             REFERENCES users(id) ON DELETE SET NULL,
    updated_by    UUID             REFERENCES users(id) ON DELETE SET NULL,
    created_at    TIMESTAMP        NOT NULL DEFAULT now(),
    updated_at    TIMESTAMP        NOT NULL DEFAULT now()
);

CREATE INDEX idx_tb_document_sort ON transcription_blocks(document_id, sort_order);
CREATE INDEX idx_tb_annotation    ON transcription_blocks(annotation_id);
