-- Migration: companies table + company_id in users

-- 1. Tabelle companies erstellen
CREATE TABLE IF NOT EXISTS public.partners (
  id         UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  name       TEXT NOT NULL,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- RLS aktivieren
ALTER TABLE public.companies ENABLE ROW LEVEL SECURITY;

-- Policies
DROP POLICY IF EXISTS "Admins und Partner lesen Partner" ON public.partners;
DROP POLICY IF EXISTS "Service role kann alles" ON public.partners;

CREATE POLICY "Admins und Partner lesen Partner"
  ON public.partners FOR SELECT
  USING (auth.role() = 'authenticated');

CREATE POLICY "Service role kann alles"
  ON public.partners FOR ALL
  USING (true)
  WITH CHECK (true);

-- 2. company_id zu public.users hinzufügen
ALTER TABLE public.users
  ADD COLUMN IF NOT EXISTS partner_id UUID REFERENCES public.partners(id) ON DELETE SET NULL;

-- Reload Schema Cache
NOTIFY pgrst, 'reload schema';
