revoke delete on table "auth"."audit_log_entries" from "postgres";

revoke insert on table "auth"."audit_log_entries" from "postgres";

revoke references on table "auth"."audit_log_entries" from "postgres";

revoke select on table "auth"."audit_log_entries" from "postgres";

revoke trigger on table "auth"."audit_log_entries" from "postgres";

revoke truncate on table "auth"."audit_log_entries" from "postgres";

revoke update on table "auth"."audit_log_entries" from "postgres";

revoke delete on table "auth"."flow_state" from "postgres";

revoke insert on table "auth"."flow_state" from "postgres";

revoke references on table "auth"."flow_state" from "postgres";

revoke select on table "auth"."flow_state" from "postgres";

revoke trigger on table "auth"."flow_state" from "postgres";

revoke truncate on table "auth"."flow_state" from "postgres";

revoke update on table "auth"."flow_state" from "postgres";

revoke delete on table "auth"."identities" from "postgres";

revoke insert on table "auth"."identities" from "postgres";

revoke references on table "auth"."identities" from "postgres";

revoke select on table "auth"."identities" from "postgres";

revoke trigger on table "auth"."identities" from "postgres";

revoke truncate on table "auth"."identities" from "postgres";

revoke update on table "auth"."identities" from "postgres";

revoke delete on table "auth"."instances" from "postgres";

revoke insert on table "auth"."instances" from "postgres";

revoke references on table "auth"."instances" from "postgres";

revoke select on table "auth"."instances" from "postgres";

revoke trigger on table "auth"."instances" from "postgres";

revoke truncate on table "auth"."instances" from "postgres";

revoke update on table "auth"."instances" from "postgres";

revoke delete on table "auth"."mfa_amr_claims" from "postgres";

revoke insert on table "auth"."mfa_amr_claims" from "postgres";

revoke references on table "auth"."mfa_amr_claims" from "postgres";

revoke select on table "auth"."mfa_amr_claims" from "postgres";

revoke trigger on table "auth"."mfa_amr_claims" from "postgres";

revoke truncate on table "auth"."mfa_amr_claims" from "postgres";

revoke update on table "auth"."mfa_amr_claims" from "postgres";

revoke delete on table "auth"."mfa_challenges" from "postgres";

revoke insert on table "auth"."mfa_challenges" from "postgres";

revoke references on table "auth"."mfa_challenges" from "postgres";

revoke select on table "auth"."mfa_challenges" from "postgres";

revoke trigger on table "auth"."mfa_challenges" from "postgres";

revoke truncate on table "auth"."mfa_challenges" from "postgres";

revoke update on table "auth"."mfa_challenges" from "postgres";

revoke delete on table "auth"."mfa_factors" from "postgres";

revoke insert on table "auth"."mfa_factors" from "postgres";

revoke references on table "auth"."mfa_factors" from "postgres";

revoke select on table "auth"."mfa_factors" from "postgres";

revoke trigger on table "auth"."mfa_factors" from "postgres";

revoke truncate on table "auth"."mfa_factors" from "postgres";

revoke update on table "auth"."mfa_factors" from "postgres";

revoke delete on table "auth"."one_time_tokens" from "postgres";

revoke insert on table "auth"."one_time_tokens" from "postgres";

revoke references on table "auth"."one_time_tokens" from "postgres";

revoke select on table "auth"."one_time_tokens" from "postgres";

revoke trigger on table "auth"."one_time_tokens" from "postgres";

revoke truncate on table "auth"."one_time_tokens" from "postgres";

revoke update on table "auth"."one_time_tokens" from "postgres";

revoke delete on table "auth"."refresh_tokens" from "postgres";

revoke insert on table "auth"."refresh_tokens" from "postgres";

revoke references on table "auth"."refresh_tokens" from "postgres";

revoke select on table "auth"."refresh_tokens" from "postgres";

revoke trigger on table "auth"."refresh_tokens" from "postgres";

revoke truncate on table "auth"."refresh_tokens" from "postgres";

revoke update on table "auth"."refresh_tokens" from "postgres";

revoke delete on table "auth"."saml_providers" from "postgres";

revoke insert on table "auth"."saml_providers" from "postgres";

revoke references on table "auth"."saml_providers" from "postgres";

revoke select on table "auth"."saml_providers" from "postgres";

revoke trigger on table "auth"."saml_providers" from "postgres";

revoke truncate on table "auth"."saml_providers" from "postgres";

revoke update on table "auth"."saml_providers" from "postgres";

revoke delete on table "auth"."saml_relay_states" from "postgres";

revoke insert on table "auth"."saml_relay_states" from "postgres";

revoke references on table "auth"."saml_relay_states" from "postgres";

revoke select on table "auth"."saml_relay_states" from "postgres";

revoke trigger on table "auth"."saml_relay_states" from "postgres";

revoke truncate on table "auth"."saml_relay_states" from "postgres";

revoke update on table "auth"."saml_relay_states" from "postgres";

revoke select on table "auth"."schema_migrations" from "postgres";

revoke delete on table "auth"."sessions" from "postgres";

revoke insert on table "auth"."sessions" from "postgres";

revoke references on table "auth"."sessions" from "postgres";

revoke select on table "auth"."sessions" from "postgres";

revoke trigger on table "auth"."sessions" from "postgres";

revoke truncate on table "auth"."sessions" from "postgres";

revoke update on table "auth"."sessions" from "postgres";

revoke delete on table "auth"."sso_domains" from "postgres";

revoke insert on table "auth"."sso_domains" from "postgres";

revoke references on table "auth"."sso_domains" from "postgres";

revoke select on table "auth"."sso_domains" from "postgres";

revoke trigger on table "auth"."sso_domains" from "postgres";

revoke truncate on table "auth"."sso_domains" from "postgres";

revoke update on table "auth"."sso_domains" from "postgres";

revoke delete on table "auth"."sso_providers" from "postgres";

revoke insert on table "auth"."sso_providers" from "postgres";

revoke references on table "auth"."sso_providers" from "postgres";

revoke select on table "auth"."sso_providers" from "postgres";

revoke trigger on table "auth"."sso_providers" from "postgres";

revoke truncate on table "auth"."sso_providers" from "postgres";

revoke update on table "auth"."sso_providers" from "postgres";

revoke delete on table "auth"."users" from "postgres";

revoke insert on table "auth"."users" from "postgres";

revoke references on table "auth"."users" from "postgres";

revoke select on table "auth"."users" from "postgres";

revoke trigger on table "auth"."users" from "postgres";

revoke truncate on table "auth"."users" from "postgres";

revoke update on table "auth"."users" from "postgres";

set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.handle_new_user()
 RETURNS trigger
 LANGUAGE plpgsql
 SECURITY DEFINER
 SET search_path TO 'public'
AS $function$
DECLARE
  organization_id UUID;
BEGIN
  -- First create organization if it doesn't exist
  INSERT INTO public.organizations (
    name,
    subscription_tier,
    subscription_status
  ) VALUES (
    COALESCE(NEW.raw_user_meta_data->>'organization_name', NEW.raw_user_meta_data->>'name' || '''s Organization'),
    'free',
    'active'
  )
  RETURNING id INTO organization_id;

  -- Then create the user profile
  INSERT INTO public.users (
    id,
    email,
    name,
    organization_id,
    role,
    is_active,
    settings,
    created_at,
    updated_at
  ) VALUES (
    NEW.id,
    NEW.email,
    COALESCE(NEW.raw_user_meta_data->>'name', split_part(NEW.email, '@', 1)),
    organization_id,
    COALESCE(NEW.raw_user_meta_data->>'role', 'viewer')::user_role,
    true,
    jsonb_build_object(
      'email_notifications', true,
      'notification_frequency', 'instant',
      'dashboard_layout', 'default'
    ),
    NOW(),
    NOW()
  );

  RETURN NEW;
EXCEPTION
  WHEN others THEN
    -- Log the error (will appear in Postgres logs)
    RAISE LOG 'Error in handle_new_user: %', SQLERRM;
    RETURN NEW;
END;
$function$
;

DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users;
CREATE TRIGGER on_auth_user_created AFTER INSERT ON auth.users FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();

revoke delete on table "storage"."buckets" from "anon";

revoke insert on table "storage"."buckets" from "anon";

revoke references on table "storage"."buckets" from "anon";

revoke select on table "storage"."buckets" from "anon";

revoke trigger on table "storage"."buckets" from "anon";

revoke truncate on table "storage"."buckets" from "anon";

revoke update on table "storage"."buckets" from "anon";

revoke delete on table "storage"."buckets" from "authenticated";

revoke insert on table "storage"."buckets" from "authenticated";

revoke references on table "storage"."buckets" from "authenticated";

revoke select on table "storage"."buckets" from "authenticated";

revoke trigger on table "storage"."buckets" from "authenticated";

revoke truncate on table "storage"."buckets" from "authenticated";

revoke update on table "storage"."buckets" from "authenticated";

revoke delete on table "storage"."buckets" from "postgres";

revoke insert on table "storage"."buckets" from "postgres";

revoke references on table "storage"."buckets" from "postgres";

revoke select on table "storage"."buckets" from "postgres";

revoke trigger on table "storage"."buckets" from "postgres";

revoke truncate on table "storage"."buckets" from "postgres";

revoke update on table "storage"."buckets" from "postgres";

revoke delete on table "storage"."buckets" from "service_role";

revoke insert on table "storage"."buckets" from "service_role";

revoke references on table "storage"."buckets" from "service_role";

revoke select on table "storage"."buckets" from "service_role";

revoke trigger on table "storage"."buckets" from "service_role";

revoke truncate on table "storage"."buckets" from "service_role";

revoke update on table "storage"."buckets" from "service_role";

revoke delete on table "storage"."objects" from "anon";

revoke insert on table "storage"."objects" from "anon";

revoke references on table "storage"."objects" from "anon";

revoke select on table "storage"."objects" from "anon";

revoke trigger on table "storage"."objects" from "anon";

revoke truncate on table "storage"."objects" from "anon";

revoke update on table "storage"."objects" from "anon";

revoke delete on table "storage"."objects" from "authenticated";

revoke insert on table "storage"."objects" from "authenticated";

revoke references on table "storage"."objects" from "authenticated";

revoke select on table "storage"."objects" from "authenticated";

revoke trigger on table "storage"."objects" from "authenticated";

revoke truncate on table "storage"."objects" from "authenticated";

revoke update on table "storage"."objects" from "authenticated";

revoke delete on table "storage"."objects" from "postgres";

revoke insert on table "storage"."objects" from "postgres";

revoke references on table "storage"."objects" from "postgres";

revoke select on table "storage"."objects" from "postgres";

revoke trigger on table "storage"."objects" from "postgres";

revoke truncate on table "storage"."objects" from "postgres";

revoke update on table "storage"."objects" from "postgres";

revoke delete on table "storage"."objects" from "service_role";

revoke insert on table "storage"."objects" from "service_role";

revoke references on table "storage"."objects" from "service_role";

revoke select on table "storage"."objects" from "service_role";

revoke trigger on table "storage"."objects" from "service_role";

revoke truncate on table "storage"."objects" from "service_role";

revoke update on table "storage"."objects" from "service_role";

revoke select on table "storage"."s3_multipart_uploads" from "anon";

revoke select on table "storage"."s3_multipart_uploads" from "authenticated";

revoke delete on table "storage"."s3_multipart_uploads" from "service_role";

revoke insert on table "storage"."s3_multipart_uploads" from "service_role";

revoke references on table "storage"."s3_multipart_uploads" from "service_role";

revoke select on table "storage"."s3_multipart_uploads" from "service_role";

revoke trigger on table "storage"."s3_multipart_uploads" from "service_role";

revoke truncate on table "storage"."s3_multipart_uploads" from "service_role";

revoke update on table "storage"."s3_multipart_uploads" from "service_role";

revoke select on table "storage"."s3_multipart_uploads_parts" from "anon";

revoke select on table "storage"."s3_multipart_uploads_parts" from "authenticated";

revoke delete on table "storage"."s3_multipart_uploads_parts" from "service_role";

revoke insert on table "storage"."s3_multipart_uploads_parts" from "service_role";

revoke references on table "storage"."s3_multipart_uploads_parts" from "service_role";

revoke select on table "storage"."s3_multipart_uploads_parts" from "service_role";

revoke trigger on table "storage"."s3_multipart_uploads_parts" from "service_role";

revoke truncate on table "storage"."s3_multipart_uploads_parts" from "service_role";

revoke update on table "storage"."s3_multipart_uploads_parts" from "service_role";


