export default function useBuilding() {
  const categories = ref([
    {
      title: "Academic Spaces",
      value: "academic-spaces",
    },
    {
      title: "Support Facilities",
      value: "support-facilities",
    },
    {
      title: "Activity & Recreation Areas",
      value: "activity-recreation-areas",
    },
    {
      title: "Utility & Sanitary",
      value: "utility-sanitary",
    },
    {
      title: "Dormitories & Lodging",
      value: "dormitories-lodging",
    },
  ]);

  const types = ref([
    {
      title: "Classroom",
      value: "classroom",
      category: "academic-spaces",
      subtitle: "Standard teaching room",
    },
    {
      title: "Laboratory",
      value: "laboratory",
      category: "academic-spaces",
      subtitle: "Science, computer, or language labs",
    },
    {
      title: "Library",
      value: "library",
      category: "academic-spaces",
      subtitle: "Reading and study area",
    },
    {
      title: "Faculty Room",
      value: "faculty_room",
      category: "academic-spaces",
      subtitle: "Shared working space for teachers",
    },
    {
      title: "Lecture Hall",
      value: "lecture_hall",
      category: "academic-spaces",
      subtitle: "Large room for lectures",
    },
    {
      title: "Tutorial Room",
      value: "tutorial_room",
      category: "academic-spaces",
      subtitle: "Small group learning or mentoring",
    },
    {
      title: "Exam Room",
      value: "exam_room",
      category: "academic-spaces",
      subtitle: "Designated exam space",
    },
    {
      title: "Office",
      value: "office",
      category: "support-facilities",
      subtitle: "Admin, registrar, finance, principal's office",
    },
    {
      title: "Clinic",
      value: "clinic",
      category: "support-facilities",
      subtitle: "School nurse or medical emergency room",
    },
    {
      title: "Storage",
      value: "storage",
      category: "support-facilities",
      subtitle: "For site supplies, janitorial, or equipment",
    },
    {
      title: "Server Room",
      value: "server_room",
      category: "support-facilities",
      subtitle: "For IT infrastructure",
    },
    {
      title: "Equipment Room",
      value: "equipment_room",
      category: "support-facilities",
      subtitle: "PE or technical equipment",
    },
    {
      title: "Gymnasium",
      value: "gymnasium",
      category: "activity-recreation-areas",
      subtitle: "Indoor sports",
    },
    {
      title: "Sports Area",
      value: "sports_area",
      category: "activity-recreation-areas",
      subtitle: "Outdoor field or court",
    },
    {
      title: "Music Room",
      value: "music_room",
      category: "activity-recreation-areas",
      subtitle: "For music classes or practice",
    },
    {
      title: "Art Room",
      value: "art_room",
      category: "activity-recreation-areas",
      subtitle: "Visual arts room",
    },
    {
      title: "Theater",
      value: "theater",
      category: "activity-recreation-areas",
      subtitle: "Stage and performance space",
    },
    {
      title: "Canteen",
      value: "canteen",
      category: "activity-recreation-areas",
      subtitle: "Food service area",
    },
    {
      title: "Toilet",
      value: "toilet",
      category: "utility-sanitary",
      subtitle: "Restroom facilities",
    },
    {
      title: "Shower Room",
      value: "shower_room",
      category: "utility-sanitary",
      subtitle: "Common in dorms or gyms",
    },
    {
      title: "Janitor Room",
      value: "janitor_room",
      category: "utility-sanitary",
      subtitle: "Cleaning supplies, mop sinks",
    },
    {
      title: "Mechanical Room",
      value: "mechanical_room",
      category: "utility-sanitary",
      subtitle: "Electrical, HVAC, plumbing systems",
    },
    {
      title: "Dorm Room",
      value: "dorm_room",
      category: "dormitories-lodging",
      subtitle: "Sleeping quarters",
    },
    {
      title: "Common Room",
      value: "common_room",
      category: "dormitories-lodging",
      subtitle: "Lounge or social area",
    },
    {
      title: "Laundry",
      value: "laundry",
      category: "dormitories-lodging",
      subtitle: "Washing facilities",
    },
  ]);

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

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

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

  function createBuilding(data: TBuildingPayload) {
    return useNuxtApp().$api("/api/buildings", {
      method: "POST",
      body: data,
    });
  }

  function updateBuildingField(id: string, field: string, value: string) {
    return useNuxtApp().$api(`/api/buildings/${id}`, {
      method: "PATCH",
      body: { field, value },
    });
  }

  function updateById(
    id: string,
    data: TBuildingUpdatePayload
  ) {
    return useNuxtApp().$api(`/api/buildings/id/${id}`, {
      method: "PATCH",
      body: data,
    });
  }

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


  function getBlocksSelection({siteId} : { siteId: string; }) {
    return useNuxtApp().$api(`/api/buildings/list/site/${siteId}`, {
      method: "GET",
    });
  }

  function getLevelsSelection({siteId, blockId} : { siteId: string; blockId: string }) {
    return useNuxtApp().$api(`/api/building-levels/list/site/${siteId}`, {
      method: "GET",
      query: {
        block: blockId
      }
    });
  }

  async function getUnitSelection({siteId, block, level} : { siteId: string; block: string; level: string }) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/building-units/site/${siteId}/block/${block}/level/${level}`,
      {
        method: "GET",
      }
    );
  }


  return {
    categories,
    types,
    getAll,
    getById,
    getByName,
    createBuilding,
    updateBuildingField,
    deleteById,
    updateById,
    getBlocksSelection,
    getLevelsSelection,
    getUnitSelection
  }
}
