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

  function createUnit(name: string, site: string, serviceType: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/hygiene-units/site/${site}`,
      {
        method: "POST",
        body: {
          name,
          serviceType,
        },
      }
    );
  }

  function updateUnit(id: string, name: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/hygiene-units/id/${id}`,
      {
        method: "PATCH",
        body: {
          name,
        },
      }
    );
  }

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

  function uploadUnits(file: File, site: string, serviceType?: string) {
    const formData = new FormData();
    formData.append("file", file);

    return useNuxtApp().$api<Record<string, any>>(
      `/api/hygiene-units/site/${site}/upload`,
      {
        method: "POST",
        body: formData,
        query: { serviceType },
      }
    );
  }

  function downloadUnits(site: string, serviceType: string) {
    return useNuxtApp().$api<Blob>(`/api/hygiene-units/site/${site}/download`, {
      method: "GET",
      responseType: "blob",
      query: { serviceType },
    });
  }

  return {
    getUnits,
    createUnit,
    updateUnit,
    deleteUnit,
    uploadUnits,
    downloadUnits,
  };
}
