import { queryClient } from 'src/config/react-query';
import {
  ticketKeys,
  setTicketListData,
  addTicketNotification,
  removeTicketNotification,
  pushTicketListData,
  dropTicketListData,
  addTicketDetailUnreadMessage,
} from 'src/pages/dashboard/tickets/queries';
import { clearChatNotifications as clearChatNotificationsService } from 'src/services/chats';
import {
  TicketAgentStatus,
  CreateTicketSocket,
  TicketTopicUpdatedSocket,
  TicketTopicDeletedSocket,
  DeleteUnassignedTicketSocket,
  TopicOfTicketUpdatedSocket,
  UpdateTicketAssignmentSocket,
  ChatMessageAgentSocket,
} from 'src/types/tickets';

import {
  chatKeys,
  removeTicketAgentStat,
} from 'src/components/dashboard/chat/queries';

export const addTicket = (userId: number, ticket: CreateTicketSocket) => {
  pushTicketListData(ticket);
  addTicketNotification(userId);
  queryClient.invalidateQueries(chatKeys.notifications.module());
};

export const updateTopic = (
  topic: TicketTopicUpdatedSocket,
  onUpdate: Function = () => null,
) => {
  queryClient.invalidateQueries(chatKeys.ticketAgentStats.all());
  queryClient.invalidateQueries(chatKeys.notifications.module());
  queryClient.invalidateQueries(ticketKeys.list.all());
  onUpdate(topic);
};

export const updateTopicOfTicket = (
  data: TopicOfTicketUpdatedSocket,
  onUpdate: Function = () => null,
) => {
  queryClient.invalidateQueries(chatKeys.ticketAgentStats.all());
  queryClient.invalidateQueries(chatKeys.notifications.module());
  queryClient.invalidateQueries(ticketKeys.list.all());
  queryClient.invalidateQueries(chatKeys.list());
  queryClient.invalidateQueries(chatKeys.detail(data.chatId));
  queryClient.invalidateQueries(chatKeys.participants(data.chatId));
  onUpdate(data);
};

export const deleteTopic = (
  topic: TicketTopicDeletedSocket,
  onDelete: Function = () => null,
) => {
  queryClient.invalidateQueries(chatKeys.ticketAgentStats.all());
  queryClient.invalidateQueries(chatKeys.notifications.module());
  queryClient.invalidateQueries(ticketKeys.list.all());
  queryClient.invalidateQueries(chatKeys.list());
  onDelete(topic);
};

export const deleteTicket = (
  userId: number,
  ticket: DeleteUnassignedTicketSocket,
  onDelete: Function = () => null,
) => {
  removeTicketNotification(userId);
  dropTicketListData(ticket?.chat?.id);
  removeTicketAgentStat(userId);
  onDelete(ticket);
};

export const updateTicketAssignment = (
  ticket: UpdateTicketAssignmentSocket,
) => {
  let queryKey = null;

  if (ticket.status === TicketAgentStatus.UNASSIGNED) {
    queryKey = ticketKeys.list.unassigned();
  } else if (ticket.status === TicketAgentStatus.ASSIGNED) {
    queryKey = ticketKeys.list.assigned();
  } else if (ticket.status === TicketAgentStatus.CLOSED) {
    queryKey = ticketKeys.list.closed();
  }

  setTicketListData(Number(ticket.chat.id), queryKey, ticket);
};

export const chatMessageAgent = (
  userId: number,
  ticket: ChatMessageAgentSocket,
  currentChatId: number,
) => {
  const chatId = ticket?.chat?.id;
  const messageUserId = ticket?.chat?.lastMessage?.userId;

  if (currentChatId !== chatId) {
    queryClient.invalidateQueries(chatKeys.ticketAgentStats.detail(userId));
    addTicketDetailUnreadMessage(chatId);
  } else if (messageUserId !== userId) {
    clearChatNotificationsService(chatId);
  }

  queryClient.invalidateQueries(ticketKeys.list.assigned());
  queryClient.invalidateQueries(ticketKeys.messages.list(chatId));
};
