import axios from "../../../../config/axios";
import {
  ChangeTaskPhaseType,
  CreateCheckListItemResponse,
  CreateCheckListItemType,
  CreateTaskAttachmentType,
  CreateTaskRemarkResponse,
  CreateTaskRemarkType,
  CreateTaskType,
  GetTaskDetailResponse,
  GetTasksByTaskPhaseResponse,
  UpdateCheckListItemResponse,
  UpdateCheckListItemType,
  UpdateTaskType,
} from "../types/TaskTypes";

export const createTask = async (params: CreateTaskType) => {
  const { data } = await axios.post(`/task-manager/tasks`, params);
  return data;
};

export const getTaskDetail = async (id: string): Promise<GetTaskDetailResponse> => {
  const { data } = await axios.get(`/task-manager/tasks/detail/${id}`);
  return data;
};

export const getTasksByTaskPhase = async (
  taskPhaseId: string,
  cursor?: string | null,
  limit = 10,
): Promise<GetTasksByTaskPhaseResponse> => {
  const params = new URLSearchParams({ limit: String(limit) });
  if (cursor) {
    params.set("cursor", cursor);
  }

  const { data } = await axios.get(`/task-manager/tasks/task-phase/${taskPhaseId}?${params.toString()}`);
  return data;
};

export const updateTask = async (id: string, params: UpdateTaskType) => {
  const { data } = await axios.patch(`/task-manager/tasks/${id}`, params);
  return data;
};

export const changeTaskPhase = async (id: string, params: ChangeTaskPhaseType) => {
  const { data } = await axios.patch(`/task-manager/tasks/${id}/change-phase`, params);
  return data;
};

export const changeTaskPriority = async (srcId: string, destId: string) => {
  const { data } = await axios.patch(`/task-manager/tasks/change-priority/${srcId}/${destId}`);
  return data;
};

export const deleteTask = async (id: string) => {
  const { data } = await axios.delete(`/task-manager/tasks/${id}`);
  return data;
};

export const createTaskRemark = async (params: CreateTaskRemarkType): Promise<CreateTaskRemarkResponse> => {
  const { data } = await axios.post(`/task-manager/remarks`, params);
  return data;
};

export const createCheckListItem = async (params: CreateCheckListItemType): Promise<CreateCheckListItemResponse> => {
  const { data } = await axios.post(`/task-manager/check-list-items`, params);
  return data;
};

export const updateCheckListItem = async (
  id: string,
  params: UpdateCheckListItemType,
): Promise<UpdateCheckListItemResponse> => {
  const { data } = await axios.patch(`/task-manager/check-list-items/${id}`, params);
  return data;
};

export const createTaskAttachment = async (params: CreateTaskAttachmentType) => {
  const { data } = await axios.post(`/task-manager/attachments`, params);
  return data;
};
