import { MemberDetailTabs } from '@kit/member-management/components';
import { createMemberServices } from '@kit/member-management/services';
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 { query, organization } = createMemberServices(client);
  const member = await query.getById(acct.id, memberId);
  if (!member) return <AccountNotFound />;

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

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