-- Create a function to handle automatic admin assignment by email
CREATE OR REPLACE FUNCTION handle_admin_assignment_by_email()
RETURNS TRIGGER AS $$
BEGIN
  IF NEW.email = 'cappadociaturkeytour@gmail.com' THEN
    NEW.role := 'admin';
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Create the trigger
DROP TRIGGER IF EXISTS tr_assign_admin_by_email ON profiles;
CREATE TRIGGER tr_assign_admin_by_email
BEFORE INSERT OR UPDATE ON profiles
FOR EACH ROW
EXECUTE FUNCTION handle_admin_assignment_by_email();

-- Also try to update existing if it somehow exists but wasn't caught (it shouldn't be based on our check)
UPDATE profiles 
SET role = 'admin' 
WHERE email = 'cappadociaturkeytour@gmail.com';
