DROP TRIGGER IF EXISTS enforce_non_temporary_on_paid_plan ON public.profiles;
DROP FUNCTION IF EXISTS public.enforce_non_temporary_on_paid_plan();

ALTER TABLE public.profiles
  DROP CONSTRAINT IF EXISTS profiles_no_temporary_on_paid_plan;

ALTER TABLE public.profiles
  DROP COLUMN IF EXISTS is_temporary;

CREATE OR REPLACE FUNCTION public.handle_new_user() RETURNS trigger
    LANGUAGE plpgsql
    SECURITY DEFINER
AS $$
  DECLARE
    name TEXT;
    first_name TEXT;
    last_name TEXT;
    is_invited_user BOOLEAN;
    email_prefix TEXT;
    assigned_plan public.subscription_plan := 'none';
  BEGIN
  -- Extract first_name and last_name from metadata
  first_name = NEW.raw_user_meta_data ->> 'first_name';
  last_name = NEW.raw_user_meta_data ->> 'last_name';

  -- If first_name is not provided, extract it from email (part before @)
  IF first_name IS NULL OR first_name = '' THEN
    email_prefix = SPLIT_PART(NEW.email, '@', 1);
    first_name = email_prefix;
  END IF;

  -- Determine the full name
  IF NEW.raw_user_meta_data ->> 'name' IS NOT NULL
  THEN
    name = NEW.raw_user_meta_data ->> 'name';
    -- If name is provided but not first/last, try to split it
    IF first_name IS NULL AND last_name IS NULL AND name IS NOT NULL THEN
      first_name = SPLIT_PART(name, ' ', 1);
      IF ARRAY_LENGTH(STRING_TO_ARRAY(name, ' '), 1) > 1 THEN
        last_name = SUBSTRING(name FROM LENGTH(SPLIT_PART(name, ' ', 1)) + 2);
      END IF;
    END IF;
  ELSE
    name = CONCAT(first_name, ' ', last_name);
  END IF;

  is_invited_user := COALESCE(NEW.raw_user_meta_data->>'role', '') = 'invited_user';

  -- Preserve previous behavior: invited users do not get an automatic free plan.
  IF NOT is_invited_user AND public.is_freemium_available() THEN
    assigned_plan := 'free';
  END IF;

  INSERT INTO public.profiles (id, name, email, avatar_url, first_name, last_name, plan)
  VALUES (NEW.id, name, NEW.email, NEW.raw_user_meta_data ->> 'avatar_url', first_name, last_name, assigned_plan);

  RETURN NEW;
END;
$$;

COMMENT ON FUNCTION public.handle_new_user() IS
'Trigger function that creates a profile when a new user is created. Extracts first name from email when missing and assigns the free plan while freemium is available, except for invited users.';

ALTER FUNCTION public.handle_new_user() OWNER TO postgres;

CREATE OR REPLACE FUNCTION public.update_tablo_invites_on_login() RETURNS trigger
    LANGUAGE plpgsql
    SECURITY DEFINER
AS $$
    BEGIN
      IF (NEW.last_sign_in_at IS NULL OR NEW.last_sign_in_at = OLD.last_sign_in_at) THEN
        RETURN NULL;
      ELSE
        -- After removing profiles.is_temporary, use the auth metadata role to
        -- preserve the previous invited-user-only invite-consumption behavior.
        UPDATE public.tablo_invites
        SET is_pending = FALSE
        WHERE invited_email = NEW.email
          AND is_pending = TRUE
          AND COALESCE(NEW.raw_user_meta_data->>'role', '') = 'invited_user';
        RETURN NEW;
      END IF;
    END;
$$;

ALTER FUNCTION public.update_tablo_invites_on_login() OWNER TO postgres;
