-- Add admin role to users table if not exists
-- This script ensures there's an admin user role available

-- First, let's check if we need to update any user to admin
-- You can manually set a user as admin by running:
-- UPDATE users SET role = 'admin' WHERE email = 'your-admin-email@example.com';

-- Create admin-specific RLS policies for viewing all data
-- Policy for admins to view all users
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies 
    WHERE tablename = 'users' AND policyname = 'Admins can view all users'
  ) THEN
    CREATE POLICY "Admins can view all users" ON users
      FOR SELECT
      USING (
        EXISTS (
          SELECT 1 FROM users WHERE id = auth.uid() AND role = 'admin'
        )
      );
  END IF;
END $$;

-- Policy for admins to update all users
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies 
    WHERE tablename = 'users' AND policyname = 'Admins can update all users'
  ) THEN
    CREATE POLICY "Admins can update all users" ON users
      FOR UPDATE
      USING (
        EXISTS (
          SELECT 1 FROM users WHERE id = auth.uid() AND role = 'admin'
        )
      );
  END IF;
END $$;

-- Policy for admins to view all events
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies 
    WHERE tablename = 'events' AND policyname = 'Admins can view all events'
  ) THEN
    CREATE POLICY "Admins can view all events" ON events
      FOR SELECT
      USING (
        EXISTS (
          SELECT 1 FROM users WHERE id = auth.uid() AND role = 'admin'
        )
      );
  END IF;
END $$;

-- Policy for admins to update all events
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies 
    WHERE tablename = 'events' AND policyname = 'Admins can update all events'
  ) THEN
    CREATE POLICY "Admins can update all events" ON events
      FOR UPDATE
      USING (
        EXISTS (
          SELECT 1 FROM users WHERE id = auth.uid() AND role = 'admin'
        )
      );
  END IF;
END $$;

-- Policy for admins to delete all events
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies 
    WHERE tablename = 'events' AND policyname = 'Admins can delete all events'
  ) THEN
    CREATE POLICY "Admins can delete all events" ON events
      FOR DELETE
      USING (
        EXISTS (
          SELECT 1 FROM users WHERE id = auth.uid() AND role = 'admin'
        )
      );
  END IF;
END $$;

-- Policy for admins to view all orders
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies 
    WHERE tablename = 'orders' AND policyname = 'Admins can view all orders'
  ) THEN
    CREATE POLICY "Admins can view all orders" ON orders
      FOR SELECT
      USING (
        EXISTS (
          SELECT 1 FROM users WHERE id = auth.uid() AND role = 'admin'
        )
      );
  END IF;
END $$;

-- Policy for admins to view all chits
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies 
    WHERE tablename = 'chits' AND policyname = 'Admins can view all chits'
  ) THEN
    CREATE POLICY "Admins can view all chits" ON chits
      FOR SELECT
      USING (
        EXISTS (
          SELECT 1 FROM users WHERE id = auth.uid() AND role = 'admin'
        )
      );
  END IF;
END $$;

-- Policy for admins to view all order tickets
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies 
    WHERE tablename = 'order_tickets' AND policyname = 'Admins can view all order tickets'
  ) THEN
    CREATE POLICY "Admins can view all order tickets" ON order_tickets
      FOR SELECT
      USING (
        EXISTS (
          SELECT 1 FROM users WHERE id = auth.uid() AND role = 'admin'
        )
      );
  END IF;
END $$;
