{"-- Add RLS policies for subscribers table to allow super admin to update subscribers
DROP POLICY IF EXISTS \"Super admin can manage all subscribers\" ON public.subscribers","DROP POLICY IF EXISTS \"Users can view their own subscriptions\" ON public.subscribers","DROP POLICY IF EXISTS \"Users can create their own subscriptions\" ON public.subscribers","DROP POLICY IF EXISTS \"Users can update their own subscriptions\" ON public.subscribers","-- Create comprehensive policies for subscribers
CREATE POLICY \"Super admin can manage all subscribers\"
ON public.subscribers
FOR ALL
USING (is_current_user_super_admin())
WITH CHECK (is_current_user_super_admin())","CREATE POLICY \"Users can view their own subscriptions\"
ON public.subscribers
FOR SELECT
USING (user_id = auth.uid())","CREATE POLICY \"Users can create their own subscriptions\"
ON public.subscribers
FOR INSERT
WITH CHECK (user_id = auth.uid())","CREATE POLICY \"Users can update their own subscriptions\"
ON public.subscribers
FOR UPDATE
USING (user_id = auth.uid())
WITH CHECK (user_id = auth.uid())","-- Add policy for admins to view all subscribers in their organization
CREATE POLICY \"Org admins can view all subscribers\"
ON public.subscribers
FOR SELECT
USING (
  EXISTS (
    SELECT 1 FROM user_roles ur
    WHERE ur.user_id = auth.uid()
    AND ur.role IN ('admin', 'manager')
    AND ur.organization_id = subscribers.organization_id
  )
  OR is_current_user_super_admin()
)","-- Add policy for admins to update subscribers in their organization
CREATE POLICY \"Org admins can update subscribers\"
ON public.subscribers
FOR UPDATE
USING (
  EXISTS (
    SELECT 1 FROM user_roles ur
    WHERE ur.user_id = auth.uid()
    AND ur.role IN ('admin', 'manager')
    AND ur.organization_id = subscribers.organization_id
  )
  OR is_current_user_super_admin()
)
WITH CHECK (
  EXISTS (
    SELECT 1 FROM user_roles ur
    WHERE ur.user_id = auth.uid()
    AND ur.role IN ('admin', 'manager')
    AND ur.organization_id = subscribers.organization_id
  )
  OR is_current_user_super_admin()
)"}