-- Migration 119: Cloud file versioning and sharing (v0.802 CL1)
-- Version history for user_files
CREATE TABLE IF NOT EXISTS cloud_file_versions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    file_id UUID NOT NULL REFERENCES user_files(id) ON DELETE CASCADE,
    version INTEGER NOT NULL DEFAULT 1,
    storage_key TEXT NOT NULL,
    size_bytes BIGINT NOT NULL DEFAULT 0,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_cloud_file_versions_file_id ON cloud_file_versions(file_id);

-- Share links for temporary access
CREATE TABLE IF NOT EXISTS cloud_file_shares (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    file_id UUID NOT NULL REFERENCES user_files(id) ON DELETE CASCADE,
    token VARCHAR(64) NOT NULL UNIQUE,
    permissions VARCHAR(20) NOT NULL DEFAULT 'read',
    expires_at TIMESTAMPTZ NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_cloud_file_shares_file_id ON cloud_file_shares(file_id);
CREATE INDEX idx_cloud_file_shares_token ON cloud_file_shares(token);
CREATE INDEX idx_cloud_file_shares_expires_at ON cloud_file_shares(expires_at);
