-- 944_create_data_exports.sql
-- v0.10.8 F065: GDPR data export (async ZIP, 7-day validity)

CREATE TABLE IF NOT EXISTS public.data_exports (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
    status VARCHAR(20) NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'processing', 'completed', 'failed')),
    s3_key TEXT,
    file_size_bytes BIGINT,
    expires_at TIMESTAMPTZ NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    completed_at TIMESTAMPTZ,
    error_message TEXT
);

CREATE INDEX IF NOT EXISTS idx_data_exports_user_id ON public.data_exports(user_id);
CREATE INDEX IF NOT EXISTS idx_data_exports_status ON public.data_exports(status);
CREATE INDEX IF NOT EXISTS idx_data_exports_expires_at ON public.data_exports(expires_at);
CREATE INDEX IF NOT EXISTS idx_data_exports_user_created ON public.data_exports(user_id, created_at DESC);

COMMENT ON TABLE public.data_exports IS 'v0.10.8 F065: GDPR data export jobs (ZIP, 7-day download)';
