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

import { PageBody } from '@kit/ui/page';
import { Trans } from '@kit/ui/trans';
import { HomeLayoutPageHeader } from '../../_components/home-page-header';
import { loadAnalyses } from '../../_lib/server/load-analyses';
import OrderAnalysesCards from '../../_components/order-analyses-cards';
import { createPageViewLog, PAGE_VIEW_ACTION } from '~/lib/services/audit/pageView.service';
import { loadCurrentUserAccount } from '../../_lib/server/load-user-account';

export const generateMetadata = async () => {
  const { t } = await createI18nServerInstance();

  return {
    title: t('order-analysis:title'),
  };
};

async function OrderAnalysisPage() {
  const account = await loadCurrentUserAccount();
  if (!account) {
    throw new Error('Account not found');
  }

  const { analyses, countryCode } = await loadAnalyses();
  
  await createPageViewLog({
    accountId: account.id,
    action: PAGE_VIEW_ACTION.VIEW_ORDER_ANALYSIS,
  });

  return (
    <>
      <HomeLayoutPageHeader
        title={<Trans i18nKey={'order-analysis:title'} />}
        description={<Trans i18nKey={'order-analysis:description'} />}
      />
      <PageBody>
        <OrderAnalysesCards analyses={analyses} countryCode={countryCode} />
      </PageBody>
    </>
  );
}

export default withI18n(OrderAnalysisPage);
