import { createTeamAccountsApi } from '@/packages/features/team-accounts/src/server/api';
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';

import { PageBody } from '@kit/ui/page';

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

import { loadTeamAccountBenefitExpensesOverview } from '../_lib/server/load-team-account-benefit-expenses-overview';
import HealthBenefitForm from './_components/health-benefit-form';

interface TeamAccountBillingPageProps {
  params: Promise<{ account: string }>;
}

export const generateMetadata = async () => {
  const i18n = await createI18nServerInstance();
  const title = i18n.t('teams:billing.pageTitle');

  return {
    title,
  };
};

async function TeamAccountBillingPage({ params }: TeamAccountBillingPageProps) {
  const accountSlug = (await params).account;
  const client = getSupabaseServerClient();
  const api = createTeamAccountsApi(client);

  const account = await api.getTeamAccount(accountSlug);
  const { members } = await api.getMembers(accountSlug);
  const [expensesOverview, companyParams] = await Promise.all([
    loadTeamAccountBenefitExpensesOverview({
      companyId: account.id,
      employeeCount: members.length,
    }),
    api.getTeamAccountParams(account.id),
  ]);

  return (
    <PageBody>
      <HealthBenefitForm
        account={account}
        companyParams={companyParams}
        employeeCount={members.length}
        expensesOverview={expensesOverview}
      />
    </PageBody>
  );
}

export default withI18n(TeamAccountBillingPage);
