import { createMemberManagementApi } from '@kit/member-management/api';
import { ApplicationWorkflow } 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 ApplicationsPage({ 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 applications = await api.listApplications(acct.id);

  return (
    <ApplicationWorkflow
      applications={applications}
      accountId={acct.id}
      account={account}
    />
  );
}
