CREATE TABLE IF NOT EXISTS public.gateways (
  id               uuid         PRIMARY KEY DEFAULT gen_random_uuid(),
  process_id       uuid         NOT NULL REFERENCES public.processes(id) ON DELETE CASCADE,
  bpmn_element_id  text         NOT NULL,
  gateway_type     text         NOT NULL CHECK (gateway_type IN ('exclusive','parallel','inclusive','event-based')),
  branches         jsonb        NOT NULL DEFAULT '[]',
  created_at       timestamptz  NOT NULL DEFAULT now(),
  updated_at       timestamptz  NOT NULL DEFAULT now()
);

CREATE UNIQUE INDEX IF NOT EXISTS gateways_process_bpmn_idx
  ON public.gateways(process_id, bpmn_element_id);

ALTER TABLE public.gateways ENABLE ROW LEVEL SECURITY;

CREATE POLICY "all_gateways" ON public.gateways
  FOR ALL USING (auth.uid() IS NOT NULL)
  WITH CHECK (auth.uid() IS NOT NULL);
