CREATE TABLE IF NOT EXISTS "tenant_export_jobs" (
    "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
    "created_at" timestamp with time zone DEFAULT now() NOT NULL,
    "updated_at" timestamp with time zone,
    "completed_at" timestamp with time zone,
    "tenant_id" bigint NOT NULL REFERENCES "tenants"("id") ON DELETE cascade,
    "created_by" uuid REFERENCES "auth_users"("id"),
    "status" text DEFAULT 'queued' NOT NULL,
    "filename" text NOT NULL,
    "storage_path" text,
    "content_type" text DEFAULT 'application/zip' NOT NULL,
    "file_size" bigint,
    "error" text,
    "files_total" integer DEFAULT 0 NOT NULL,
    "files_done" integer DEFAULT 0 NOT NULL
);

CREATE INDEX IF NOT EXISTS "tenant_export_jobs_tenant_status_idx"
    ON "tenant_export_jobs" ("tenant_id", "status");
