CREATE TABLE IF NOT EXISTS public.processes (
  id                      uuid         PRIMARY KEY DEFAULT gen_random_uuid(),
  name                    text         NOT NULL,
  client                  text         NOT NULL DEFAULT '',
  bpmn_xml                text         NOT NULL DEFAULT '',
  currency                text         NOT NULL DEFAULT 'USD',
  overhead_percentage     numeric      NOT NULL DEFAULT 0,
  annual_frequency        numeric      NOT NULL DEFAULT 1000,
  analysis_horizon_years  numeric      NOT NULL DEFAULT 3,
  automation_investment   numeric      NOT NULL DEFAULT 0,
  group_id                uuid         REFERENCES public.process_groups(id) ON DELETE SET NULL,
  tags                    text[]       NOT NULL DEFAULT '{}',
  owner_id                uuid         NOT NULL REFERENCES public.users(id),
  created_by              uuid         NOT NULL REFERENCES public.users(id),
  updated_by              uuid         NOT NULL REFERENCES public.users(id),
  created_at              timestamptz  NOT NULL DEFAULT now(),
  updated_at              timestamptz  NOT NULL DEFAULT now()
);

ALTER TABLE public.processes ENABLE ROW LEVEL SECURITY;

-- En Sprint 4 todos los usuarios son platform_admin: ven todo.
-- Las políticas granulares (process_access) se agregan en Etapa 5.
CREATE POLICY "authenticated_all_processes" ON public.processes
  FOR ALL USING (auth.uid() IS NOT NULL)
  WITH CHECK (auth.uid() IS NOT NULL);
