export default function useScheduleTask() {
  async function getScheduleTasks({
    site = "",
    search = "",
    page = 1,
    limit = 10,
    serviceType = "",
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/hygiene-schedule-tasks/site/${site}`,
      {
        method: "GET",
        query: {
          site,
          search,
          page,
          limit,
          serviceType,
        },
      },
    );
  }

  function getScheduleTaskById(id: string) {
    return useNuxtApp().$api<TScheduleTask>(
      `/api/hygiene-schedule-tasks/id/${id}`,
      {
        method: "GET",
      },
    );
  }

  function createScheduleTask(payload: {
    title: string;
    time: string;
    dates: string[];
    description?: string;
    areas: { name: string; value: string }[];
    site: string;
    serviceType: string;
  }) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/hygiene-schedule-tasks/site/${payload.site}`,
      {
        method: "POST",
        body: {
          title: payload.title,
          time: payload.time,
          dates: payload.dates,
          description: payload.description,
          areas: payload.areas,
          serviceType: payload.serviceType,
        },
      },
    );
  }

  function updateScheduleTask(
    id: string,
    payload: {
      title: string;
      time: string;
      dates: string[];
      description?: string;
      areas: { name: string; value: string }[];
    },
  ) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/hygiene-schedule-tasks/id/${id}`,
      {
        method: "PATCH",
        body: {
          title: payload.title,
          time: payload.time,
          dates: payload.dates,
          description: payload.description,
          areas: payload.areas,
        },
      },
    );
  }

  function deleteScheduleTask(id: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/hygiene-schedule-tasks/id/${id}`,
      {
        method: "DELETE",
      },
    );
  }

  return {
    getScheduleTasks,
    createScheduleTask,
    updateScheduleTask,
    getScheduleTaskById,
    deleteScheduleTask,
  };
}
