import { getCustomer } from '@/actions/api/customers';
import { getSessionUser } from '@/actions/session';
import { Container } from '@/components/layout';
import { PageHeader } from '@/components/navigation';
import { ContactDataCard, PersonCard, ProfileOrdersList } from '@/components/profile';
import { ReadonlyServicesList } from '@/components/profile/services';
import { dehydrate, HydrationBoundary, QueryClient } from '@tanstack/react-query';

// Тип параметров страницы
type Props = { params: Promise<{ telegramId: string }> };

export default async function ProfilePage(props: Readonly<Props>) {
  const { telegramId } = await props.params;
  const contactTelegramId = Number(telegramId);
  const queryClient = new QueryClient();

  // Получаем профиль контакта
  const { customer: profile } = await queryClient.fetchQuery({
    queryFn: () => getCustomer({ telegramId: contactTelegramId }),
    queryKey: ['customer', contactTelegramId],
  });

  // Получаем текущего пользователя
  const sessionUser = await getSessionUser();
  const { customer: currentUser } = await queryClient.fetchQuery({
    queryFn: () => getCustomer({ telegramId: sessionUser.telegramId }),
    queryKey: ['customer', sessionUser.telegramId],
  });

  // Проверка наличия данных
  if (!profile || !currentUser) return null;

  return (
    <HydrationBoundary state={dehydrate(queryClient)}>
      <PageHeader title="Профиль контакта" />
      <Container className="px-0">
        <PersonCard telegramId={contactTelegramId} />
        <ContactDataCard telegramId={contactTelegramId} />
        <ReadonlyServicesList masterId={profile.documentId} />
        <ProfileOrdersList telegramId={contactTelegramId} />
      </Container>
    </HydrationBoundary>
  );
}
