import { redirect } from 'next/navigation';

import pathsConfig from '@/config/paths.config';
import { useTranslation } from 'react-i18next';

import { usePersonalAccountData } from '@kit/accounts/hooks/use-personal-account-data';
import { SuccessNotification } from '@kit/notifications/components';
import { getSupabaseServerClient } from '@kit/supabase/server-client';

import { withI18n } from '~/lib/i18n/with-i18n';

async function UpdateAccountSuccess() {
  const { t } = useTranslation('account');
  const client = getSupabaseServerClient();

  const {
    data: { user },
  } = await client.auth.getUser();

  if (!user?.id) {
    redirect(pathsConfig.app.home);
  }

  const { data: accountData } = usePersonalAccountData(user.id);

  if (!accountData?.id) {
    redirect(pathsConfig.app.home);
  }

  return (
    <SuccessNotification
      showLogo={false}
      title={t('account:membershipConfirmation:successTitle', {
        firstName: accountData?.name,
        lastName: accountData?.last_name,
      })}
      descriptionKey="account:membershipConfirmation:successDescription"
      buttonProps={{
        buttonTitleKey: 'account:membershipConfirmation:successButton',
        href: pathsConfig.app.selectPackage,
      }}
    />
  );
}

export default withI18n(UpdateAccountSuccess);
