import { createMemberManagementApi } from '@kit/member-management/api';
import { MemberCreateWizard } from '@kit/member-management/components';
import { getSupabaseServerClient } from '@kit/supabase/server-client';

import { AccountNotFound } from '~/components/account-not-found';

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

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

  const api = createMemberManagementApi(client);
  const duesCategories = await api.listDuesCategories(acct.id);

  return (
    <MemberCreateWizard
      accountId={acct.id}
      account={account}
      duesCategories={(duesCategories ?? []).map(
        (c: Record<string, unknown>) => ({
          id: String(c.id),
          name: String(c.name),
          amount: Number(c.amount ?? 0),
        }),
      )}
    />
  );
}
