import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import * as api from "../service/TaskService";
import {
  ChangeTaskPhaseType,
  CreateCheckListItemType,
  CreateTaskAttachmentType,
  CreateTaskRemarkType,
  CreateTaskType,
  UpdateCheckListItemType,
  UpdateTaskType,
} from "../types/TaskTypes";

export const useGetTaskDetail = (id: string, enabled = true) => {
  return useQuery({
    queryKey: ["task-detail", id],
    queryFn: () => api.getTaskDetail(id),
    enabled: !!id && enabled,
  });
};

export const useGetTasksByTaskPhase = (taskPhaseId: string, enabled = true) => {
  return useInfiniteQuery({
    queryKey: ["tasks-by-task-phase", taskPhaseId],
    queryFn: ({ pageParam }) => api.getTasksByTaskPhase(taskPhaseId, pageParam),
    initialPageParam: undefined as string | undefined,
    getNextPageParam: (lastPage) => {
      const pager = lastPage.data?.pager;
      if (!pager?.hasNextPage) return undefined;
      return pager.nextCursor ?? undefined;
    },
    enabled: !!taskPhaseId && enabled,
  });
};

export const useCreateTask = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (variables: CreateTaskType) => api.createTask(variables),
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ["project", variables.projectId] });
      queryClient.invalidateQueries({ queryKey: ["tasks-by-task-phase", variables.taskPhaseId] });
    },
  });
};

export const useUpdateTask = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id, params }: { id: string; params: UpdateTaskType }) => api.updateTask(id, params),
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ["task-detail", variables.id] });
      queryClient.invalidateQueries({ queryKey: ["project"] });
      queryClient.invalidateQueries({ queryKey: ["tasks-by-task-phase"] });
    },
  });
};

export const useChangeTaskPhase = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id, params }: { id: string; params: ChangeTaskPhaseType; projectId: string }) =>
      api.changeTaskPhase(id, params),
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ["task-detail", variables.id] });
      queryClient.invalidateQueries({ queryKey: ["project", variables.projectId] });
      queryClient.invalidateQueries({ queryKey: ["project"] });
      queryClient.invalidateQueries({ queryKey: ["tasks-by-task-phase", variables.params.taskPhaseId] });
    },
  });
};

export const useChangeTaskPriority = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({
      srcId,
      destId,
    }: {
      srcId: string;
      destId: string;
      taskPhaseId: string;
      projectId: string;
    }) => api.changeTaskPriority(srcId, destId),
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ["tasks-by-task-phase", variables.taskPhaseId] });
    },
  });
};

export const useDeleteTask = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id }: { id: string; projectId: string }) => api.deleteTask(id),
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ["task-detail", variables.id] });
      queryClient.invalidateQueries({ queryKey: ["project", variables.projectId] });
      queryClient.invalidateQueries({ queryKey: ["project"] });
      queryClient.invalidateQueries({ queryKey: ["tasks-by-task-phase"] });
    },
  });
};

export const useCreateTaskRemark = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (variables: CreateTaskRemarkType) => api.createTaskRemark(variables),
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ["task-detail", variables.taskId] });
      queryClient.invalidateQueries({ queryKey: ["project"] });
    },
  });
};

export const useCreateCheckListItem = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (variables: CreateCheckListItemType) => api.createCheckListItem(variables),
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ["task-detail", variables.taskId] });
      queryClient.invalidateQueries({ queryKey: ["project"] });
    },
  });
};

export const useUpdateCheckListItem = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id, params }: { id: string; params: UpdateCheckListItemType; taskId: string }) =>
      api.updateCheckListItem(id, params),
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ["task-detail", variables.taskId] });
      queryClient.invalidateQueries({ queryKey: ["project"] });
    },
  });
};

export const useCreateTaskAttachment = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (variables: CreateTaskAttachmentType) => api.createTaskAttachment(variables),
    onSuccess: (_data, variables) => {
      queryClient.invalidateQueries({ queryKey: ["task-detail", variables.taskId] });
      queryClient.invalidateQueries({ queryKey: ["project"] });
    },
  });
};
