-- Drop the problematic functions that try to call edge functions from database
DROP FUNCTION IF EXISTS public.migrate_existing_users_to_novu();
DROP FUNCTION IF EXISTS public.register_novu_subscriber(uuid);

-- Update handle_new_user to only create the profile (remove Novu registration)
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS trigger
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public
AS $$
BEGIN
  -- Create profile record
  INSERT INTO public.profiles (user_id, username, display_name)
  VALUES (
    NEW.id,
    COALESCE(NEW.raw_user_meta_data ->> 'username', 'user_' || substring(NEW.id::text, 1, 8)),
    COALESCE(NEW.raw_user_meta_data ->> 'display_name', NEW.raw_user_meta_data ->> 'name')
  );

  -- Novu registration will be handled by the frontend
  RETURN NEW;
END;
$$;

COMMENT ON FUNCTION public.handle_new_user IS 'Creates user profile on signup. Novu registration is handled by frontend.';