import { mutationOptions } from '@tanstack/react-query';
import { getQueryClient } from '@/lib/query-client';
import {
  assignEnquiry,
  createEnquiry,
  createEnquiryFollowup,
  deleteEnquiry,
  deleteEnquiryFollowup,
  reassignEnquiry,
  updateEnquiry,
  updateEnquiryFollowup
} from './service';
import { enquiryKeys } from './queries';
import type {
  EnquiryAssignmentMutationPayload,
  EnquiryFollowupMutationPayload,
  EnquiryMutationPayload
} from './types';
import { crmDashboardKeys } from '@/features/crm/dashboard/api/queries';

async function invalidateEnquiryLists() {
  await getQueryClient().invalidateQueries({ queryKey: enquiryKeys.lists() });
}

async function invalidateDashboardQueries() {
  await getQueryClient().invalidateQueries({ queryKey: crmDashboardKeys.all });
}

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

async function invalidateEnquiryProjectParties(id: string) {
  await getQueryClient().invalidateQueries({ queryKey: enquiryKeys.projectParties(id) });
}

async function invalidateEnquiryFollowups(id: string) {
  await getQueryClient().invalidateQueries({ queryKey: enquiryKeys.followups(id) });
}

export async function invalidateEnquiryMutationQueries(id: string) {
  await Promise.all([
    invalidateEnquiryLists(),
    invalidateEnquiryDetail(id),
    invalidateEnquiryProjectParties(id),
    invalidateDashboardQueries()
  ]);
}

export async function invalidateEnquiryFollowupMutationQueries(enquiryId: string) {
  await Promise.all([
    invalidateEnquiryLists(),
    invalidateEnquiryDetail(enquiryId),
    invalidateEnquiryFollowups(enquiryId),
    invalidateDashboardQueries()
  ]);
}

export const createEnquiryMutation = mutationOptions({
  mutationFn: (data: EnquiryMutationPayload) => createEnquiry(data),
  onSettled: async (_data, error) => {
    if (!error) {
      await Promise.all([invalidateEnquiryLists(), invalidateDashboardQueries()]);
    }
  }
});

export const updateEnquiryMutation = mutationOptions({
  mutationFn: ({ id, values }: { id: string; values: EnquiryMutationPayload }) =>
    updateEnquiry(id, values),
  onSettled: async (_data, error, variables) => {
    if (!error) {
      await invalidateEnquiryMutationQueries(variables.id);
    }
  }
});

export const deleteEnquiryMutation = mutationOptions({
  mutationFn: (id: string) => deleteEnquiry(id),
  onSettled: async (_data, error, id) => {
    if (!error) {
      getQueryClient().removeQueries({ queryKey: enquiryKeys.detail(id) });
      getQueryClient().removeQueries({ queryKey: enquiryKeys.projectParties(id) });
      getQueryClient().removeQueries({ queryKey: enquiryKeys.followups(id) });
      await Promise.all([invalidateEnquiryLists(), invalidateDashboardQueries()]);
    }
  }
});

export const assignEnquiryMutation = mutationOptions({
  mutationFn: ({ id, values }: { id: string; values: EnquiryAssignmentMutationPayload }) =>
    assignEnquiry(id, values),
  onSettled: async (_data, error, variables) => {
    if (!error) {
      await invalidateEnquiryMutationQueries(variables.id);
    }
  }
});

export const reassignEnquiryMutation = mutationOptions({
  mutationFn: ({ id, values }: { id: string; values: EnquiryAssignmentMutationPayload }) =>
    reassignEnquiry(id, values),
  onSettled: async (_data, error, variables) => {
    if (!error) {
      await invalidateEnquiryMutationQueries(variables.id);
    }
  }
});

export const createEnquiryFollowupMutation = mutationOptions({
  mutationFn: ({
    enquiryId,
    values
  }: {
    enquiryId: string;
    values: EnquiryFollowupMutationPayload;
  }) => createEnquiryFollowup(enquiryId, values),
  onSettled: async (_data, error, variables) => {
    if (!error) {
      await invalidateEnquiryFollowupMutationQueries(variables.enquiryId);
    }
  }
});

export const updateEnquiryFollowupMutation = mutationOptions({
  mutationFn: ({
    enquiryId,
    followupId,
    values
  }: {
    enquiryId: string;
    followupId: string;
    values: EnquiryFollowupMutationPayload;
  }) => updateEnquiryFollowup(enquiryId, followupId, values),
  onSettled: async (_data, error, variables) => {
    if (!error) {
      await invalidateEnquiryFollowupMutationQueries(variables.enquiryId);
    }
  }
});

export const deleteEnquiryFollowupMutation = mutationOptions({
  mutationFn: ({ enquiryId, followupId }: { enquiryId: string; followupId: string }) =>
    deleteEnquiryFollowup(enquiryId, followupId),
  onSettled: async (_data, error, variables) => {
    if (!error) {
      await invalidateEnquiryFollowupMutationQueries(variables.enquiryId);
    }
  }
});
