import { createMemberManagementApi } from '@kit/member-management/api';
import { MemberDetailTabs } 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; memberId: string }>;
}

export default async function MemberDetailPage({ params }: Props) {
  const { account, memberId } = 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 member = await api.getMember(acct.id, memberId);
  if (!member) return <AccountNotFound />;

  const [roles, honors, mandates] = await Promise.all([
    api.listMemberRoles(memberId),
    api.listMemberHonors(memberId),
    api.listMandates(memberId),
  ]);

  return (
    <MemberDetailTabs
      member={member}
      account={account}
      accountId={acct.id}
      roles={roles}
      honors={honors}
      mandates={mandates}
    />
  );
}
