-- Fix: Allow new users to create clinics and profiles during registration
-- The original RLS policies required a clinic_id which doesn't exist yet at signup time

-- Allow any authenticated user to create a clinic (for registration flow)
DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies WHERE policyname = 'Authenticated users can create clinics' AND tablename = 'clinics'
  ) THEN
    CREATE POLICY "Authenticated users can create clinics" ON clinics
      FOR INSERT WITH CHECK (auth.uid() IS NOT NULL);
  END IF;
END $$;

-- Allow users to insert their own profile row
DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies WHERE policyname = 'Users can insert own profile' AND tablename = 'users'
  ) THEN
    CREATE POLICY "Users can insert own profile" ON users
      FOR INSERT WITH CHECK (id = auth.uid());
  END IF;
END $$;
