CREATE TABLE IF NOT EXISTS export_jobs (
  id TEXT PRIMARY KEY,
  job_type TEXT NOT NULL,
  status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'running', 'completed', 'failed')),
  requested_by TEXT,
  request_json JSONB NOT NULL DEFAULT '{}'::jsonb,
  result_content_type TEXT,
  result_filename TEXT,
  result_body TEXT,
  error_message TEXT,
  created_at TIMESTAMPTZ NOT NULL,
  started_at TIMESTAMPTZ,
  completed_at TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_export_jobs_status ON export_jobs (status, created_at DESC);
