
-- Table to persist import job progress across browser sessions
CREATE TABLE public.import_jobs (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  type text NOT NULL CHECK (type IN ('addresses', 'fiber')),
  source_url text,
  status text NOT NULL DEFAULT 'running' CHECK (status IN ('running', 'done', 'error', 'paused')),
  skip_rows integer NOT NULL DEFAULT 0,
  total_inserted integer NOT NULL DEFAULT 0,
  total_errors integer NOT NULL DEFAULT 0,
  error_messages text[] DEFAULT '{}',
  logs text[] DEFAULT '{}',
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now(),
  user_id uuid NOT NULL
);

ALTER TABLE public.import_jobs ENABLE ROW LEVEL SECURITY;

-- Only admins can access import jobs
CREATE POLICY "Admins can manage import_jobs"
  ON public.import_jobs
  FOR ALL
  TO authenticated
  USING (public.has_role(auth.uid(), 'admin'))
  WITH CHECK (public.has_role(auth.uid(), 'admin'));
