export default function useBuildingUnit() {
  const categories = ref([
    {
      title: "Management",
      value: "management",
    },
    {
      title: "Residential",
      value: "residential",
    },
    {
      title: "Commercial",
      value: "commercial",
    },
    {
      title: "Amenities",
      value: "amenities",
    },
  ]);

  function getAll({
    page = 1,
    search = "",
    limit = 20,
    status = "active",
    site = "",
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>("/api/building-units", {
      method: "GET",
      query: {
        page,
        search,
        limit,
        status,
        site,
      },
    });
  }

  function getAllUnits({
    page = 1,
    search = "",
    limit = 20,
    status = "active",
    site = "",
    building = "",
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>("/api/building-units", {
      method: "GET",
      query: {
        page,
        search,
        limit,
        status,
        site,
        building,
      },
    });
  }

  function getById(id = "") {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/building-units/id/${id}`,
      {
        method: "GET",
      }
    );
  }

  function getByName(name = "") {
    return useNuxtApp().$api(`/api/building-units/name/${name}`, {
      method: "GET",
    });
  }

  function add(data: { labels: string[]; unit: Partial<TBuildingUnit>; qty?: number }) {
    return useNuxtApp().$api("/api/building-units", {
      method: "POST",
      body: data,
    });
  }

  type TUpdateOption = Pick<TBuildingUnit, "name" | "category" | "level">;

  function updateById(id: string, value: TUpdateOption) {
    return useNuxtApp().$api(`/api/building-units/id/${id}`, {
      method: "PATCH",
      body: value,
    });
  }

  function deleteById(id: string) {
    return useNuxtApp().$api(`/api/building-units/${id}`, {
      method: "PUT",
    });
  }

  const region = ref({
    _id: "",
    name: "",
    director: "",
    directorName: "",
  });

  return {
    categories,
    region,
    getAll,
    getById,
    getByName,
    add,
    updateById,
    deleteById,
    getAllUnits,
  };
}
