import { redirect } from 'next/navigation';

import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
import { withI18n } from '~/lib/i18n/with-i18n';
import { PageBody, PageHeader } from '@kit/ui/page';
import { Trans } from '@kit/ui/trans';
import { toTitleCase } from '@/lib/utils';

import Dashboard from '../_components/dashboard';
import { loadCurrentUserAccount } from '../_lib/server/load-user-account';

export const generateMetadata = async () => {
  const i18n = await createI18nServerInstance();
  const title = i18n.t('account:homePage');

  return {
    title,
  };
};

async function UserHomePage() {
  const account = await loadCurrentUserAccount();
  if (!account) {
    redirect('/');
  }

  return (
    <>
      <PageHeader title={
        <>
          <Trans i18nKey={'common:welcome'} />
          {account.name ? `, ${toTitleCase(account.name)}` : ''}
        </>
      }
        description={
          <Trans i18nKey={'dashboard:recentlyCheckedDescription'} />
        }
      />
      <PageBody>
        <Dashboard />
      </PageBody>
    </>
  );
}

export default withI18n(UserHomePage);
