import { mutationOptions } from '@tanstack/react-query';
import { getQueryClient } from '@/lib/query-client';
import {
  createCustomer,
  createCustomerContact,
  deleteCustomer,
  deleteCustomerContact,
  updateCustomer,
  updateCustomerContact
} from './service';
import { customerKeys } from './queries';
import type { CustomerContactMutationPayload, CustomerMutationPayload } from './types';

async function invalidateCustomerLists() {
  await getQueryClient().invalidateQueries({ queryKey: customerKeys.lists() });
}

async function invalidateCustomerDetail(id: string) {
  await getQueryClient().invalidateQueries({ queryKey: customerKeys.detail(id) });
}

async function invalidateCustomerContacts(id: string) {
  await getQueryClient().invalidateQueries({ queryKey: customerKeys.contacts(id) });
}

export async function invalidateCustomerMutationQueries(id: string) {
  await Promise.all([invalidateCustomerLists(), invalidateCustomerDetail(id)]);
}

export async function invalidateCustomerContactMutationQueries(customerId: string) {
  await Promise.all([
    invalidateCustomerLists(),
    invalidateCustomerDetail(customerId),
    invalidateCustomerContacts(customerId)
  ]);
}

export const createCustomerMutation = mutationOptions({
  mutationFn: (data: CustomerMutationPayload) => createCustomer(data),
  onSettled: async (_data, error) => {
    if (!error) {
      await invalidateCustomerLists();
    }
  }
});

export const updateCustomerMutation = mutationOptions({
  mutationFn: ({ id, values }: { id: string; values: CustomerMutationPayload }) =>
    updateCustomer(id, values),
  onSettled: async (_data, error, variables) => {
    if (!error) {
      await invalidateCustomerMutationQueries(variables.id);
    }
  }
});

export const deleteCustomerMutation = mutationOptions({
  mutationFn: (id: string) => deleteCustomer(id),
  onSettled: async (_data, error, id) => {
    if (!error) {
      getQueryClient().removeQueries({ queryKey: customerKeys.detail(id) });
      getQueryClient().removeQueries({ queryKey: customerKeys.contacts(id) });
      await invalidateCustomerLists();
    }
  }
});

export const createCustomerContactMutation = mutationOptions({
  mutationFn: ({
    customerId,
    values
  }: {
    customerId: string;
    values: CustomerContactMutationPayload;
  }) => createCustomerContact(customerId, values),
  onSettled: async (_data, error, variables) => {
    if (!error) {
      await invalidateCustomerContactMutationQueries(variables.customerId);
    }
  }
});

export const updateCustomerContactMutation = mutationOptions({
  mutationFn: ({
    customerId,
    contactId,
    values
  }: {
    customerId: string;
    contactId: string;
    values: CustomerContactMutationPayload;
  }) => updateCustomerContact(customerId, contactId, values),
  onSettled: async (_data, error, variables) => {
    if (!error) {
      await invalidateCustomerContactMutationQueries(variables.customerId);
    }
  }
});

export const deleteCustomerContactMutation = mutationOptions({
  mutationFn: ({ customerId, contactId }: { customerId: string; contactId: string }) =>
    deleteCustomerContact(customerId, contactId),
  onSettled: async (_data, error, variables) => {
    if (!error) {
      await invalidateCustomerContactMutationQueries(variables.customerId);
    }
  }
});
