import { CreditCard } from 'lucide-react';
import { getTranslations } from 'next-intl/server';

import { createMemberServices } from '@kit/member-management/services';
import { getSupabaseServerClient } from '@kit/supabase/server-client';

import { AccountNotFound } from '~/components/account-not-found';
import { CmsPageShell } from '~/components/cms-page-shell';
import { EmptyState } from '~/components/empty-state';

/** All active members are fetched for the card overview. */
const CARDS_PAGE_SIZE = 100;

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

export default async function MemberCardsPage({ params }: Props) {
  const { account } = await params;
  const client = getSupabaseServerClient();
  const t = await getTranslations('members');
  const { data: acct } = await client
    .from('accounts')
    .select('id')
    .eq('slug', account)
    .single();
  if (!acct) return <AccountNotFound />;

  const { query } = createMemberServices(client);
  const result = await query.list(acct.id, {
    status: 'active',
    pageSize: CARDS_PAGE_SIZE,
  });
  const members = result.data;

  return (
    <CmsPageShell
      account={account}
      title={t('cards.title')}
      description={t('cards.subtitle')}
    >
      {members.length === 0 ? (
        <EmptyState
          icon={<CreditCard className="h-8 w-8" />}
          title={t('cards.noMembers')}
          description={t('cards.noMembersDesc')}
          actionLabel={t('nav.members')}
          actionHref={`/home/${account}/members-cms`}
        />
      ) : (
        <EmptyState
          icon={<CreditCard className="h-8 w-8" />}
          title={t('cards.inDevelopment')}
          description={t('cards.inDevelopmentDesc', { count: result.total })}
          actionLabel={t('nav.members')}
          actionHref={`/home/${account}/members-cms`}
        />
      )}
    </CmsPageShell>
  );
}
