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

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

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

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

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

  function uploadAreas(file: File, site: string, serviceType?: string) {
    const formData = new FormData();
    formData.append("file", file);
    return useNuxtApp().$api<Record<string, any>>(
      `/api/hygiene-areas/site/${site}/upload`,
      {
        method: "POST",
        body: formData,
        query: { serviceType },
      },
    );
  }

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

  return {
    getAreas,
    createArea,
    updateArea,
    deleteArea,
    getAreaById,
    uploadAreas,
    exportAreas,
  };
}
