-- migrations/0005_files.sql
-- Phase 5: Files (tablo file attachments)

-- +goose Up

CREATE TABLE tablo_files (
    id           uuid        PRIMARY KEY DEFAULT gen_random_uuid(),
    tablo_id     uuid        NOT NULL REFERENCES tablos(id) ON DELETE CASCADE,
    s3_key       text        NOT NULL,
    filename     text        NOT NULL,
    content_type text        NOT NULL DEFAULT 'application/octet-stream',
    size_bytes   bigint      NOT NULL DEFAULT 0,
    created_at   timestamptz NOT NULL DEFAULT now()
);

-- Composite index: list files for a tablo ordered newest-first (D-06: files immutable).
CREATE INDEX tablo_files_tablo_id_idx ON tablo_files(tablo_id, created_at DESC);

-- +goose Down
DROP TABLE IF EXISTS tablo_files;
