-- Function to handle user signup and create user record
CREATE OR REPLACE FUNCTION handle_auth_signup()
RETURNS TRIGGER AS $$
BEGIN
  -- Create user record in users table
  INSERT INTO users (id, email, name)
  VALUES (NEW.id, NEW.email, NEW.raw_user_meta_data->>'name');
  
  RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

-- Trigger to run the function when a user signs up
CREATE TRIGGER on_auth_user_created
  AFTER INSERT ON auth.users
  FOR EACH ROW EXECUTE FUNCTION handle_auth_signup();