import { queryOptions } from '@tanstack/react-query';
import { getCustomerById, getCustomerContacts, getCustomers } from './service';
import type { CustomerFilters } from './types';

export const customerKeys = {
  all: ['crm-customers'] as const,
  lists: () => [...customerKeys.all, 'list'] as const,
  list: (filters: CustomerFilters) => [...customerKeys.lists(), filters] as const,
  details: () => [...customerKeys.all, 'detail'] as const,
  detail: (id: string) => [...customerKeys.details(), id] as const,
  contactsRoot: () => [...customerKeys.all, 'contacts'] as const,
  contacts: (id: string) => [...customerKeys.contactsRoot(), id] as const
};

export const customersQueryOptions = (filters: CustomerFilters) =>
  queryOptions({
    queryKey: customerKeys.list(filters),
    queryFn: () => getCustomers(filters)
  });

export const customerByIdOptions = (id: string) =>
  queryOptions({
    queryKey: customerKeys.detail(id),
    queryFn: () => getCustomerById(id)
  });

export const customerContactsOptions = (id: string) =>
  queryOptions({
    queryKey: customerKeys.contacts(id),
    queryFn: () => getCustomerContacts(id)
  });
