/* eslint-disable canonical/id-match */
'use client';

import { OrderCard } from '@/components/shared/order-card';
import { useCustomerQuery } from '@/hooks/api/customers';
import { useOrdersQuery } from '@/hooks/api/orders';
import { Enum_Customer_Role } from '@repo/graphql/types';

export function ClientsOrdersList() {
  const { data: { customer } = {} } = useCustomerQuery();

  const isMaster = customer?.role === Enum_Customer_Role.Master;

  const { data: { orders } = {}, isLoading } = useOrdersQuery({
    filters: {
      slot: {
        master: {
          documentId: {
            eq: isMaster ? customer?.documentId : undefined,
          },
        },
      },
    },
  });

  if (!orders?.length || isLoading) return null;

  return (
    <div className="flex flex-col space-y-2">
      <h1 className="font-bold">Записи клиентов</h1>
      {orders?.map((order) => order && <OrderCard key={order.documentId} {...order} />)}
    </div>
  );
}

export function OrdersList() {
  const { data: { customer } = {} } = useCustomerQuery();

  const { data: { orders } = {}, isLoading } = useOrdersQuery({
    filters: {
      client: {
        documentId: {
          eq: customer?.documentId,
        },
      },
    },
  });

  if (!orders?.length || isLoading) return null;

  return (
    <div className="flex flex-col space-y-2">
      <h1 className="font-bold">Ваши записи</h1>
      {orders?.map((order) => order && <OrderCard key={order.documentId} {...order} />)}
    </div>
  );
}
