export default function useFacilityCalendar() {
  // State management
  const facilities = useState<any[]>("calendarFacilities", () => []);
  const selectedFacility = useState<string>(
    "calendarSelectedFacility",
    () => ""
  );
  const isLoadingFacilities = useState<boolean>(
    "isLoadingCalendarFacilities",
    () => false
  );

  /**
   * Fetch facilities for calendar dropdown
   * @param sites - Array of site IDs to filter facilities
   * @returns Promise with facilities data
   */
  async function getFacilitiesForCalendar(
    sites: string[] = [],
    userType: string = ""
  ) {
    try {
      isLoadingFacilities.value = true;

      const response = await useNuxtApp().$api("/api/facilities/search/v1", {
        method: "POST",
        body: {
          page: 1,
          limit: 10000,
          sites,
          userType,
        },
      });

      facilities.value = response.items || [];
      isLoadingFacilities.value = false;

      return response;
    } catch (error: any) {
      isLoadingFacilities.value = false;
      facilities.value = [];
      return Promise.reject(
        error?.value?.data?.message || "Failed to fetch facilities"
      );
    }
  }

  /**
   * Get booking calendar for a specific facility, site, and date
   * @param facility - The facility ID
   * @param site - The site ID
   * @param date - The date for the calendar (ISO string format)
   * @returns Promise with booking calendar data
   */
  async function getBookingCalendar({
    facility,
    site,
    date,
  }: {
    facility: string;
    site: string;
    date: string;
  }) {
    return useNuxtApp().$api("/api/facilities-booking/booking-calendar/v1", {
      method: "POST",
      body: {
        facility,
        site,
        date,
      },
    });
  }

  async function createBooking({
    facility,
    timeSlots,
    user,
    date,
    pax,
    site,
    company,
    fullName,
    email,
    phoneNumber,
    bookingPolicy,
    memberId,
    notes,
  }: {
    facility: string;
    timeSlots: { _id: string; slot: string }[];
    user: string;
    site: string;
    date: any;
    pax: number;
    company?: string;
    fullName?: string;
    email?: string;
    phoneNumber?: string;
    bookingPolicy?: string;
    memberId?: string;
    notes?: string;
  }) {
    const data: any = {
      facility,
      timeSlots,
      user,
      date,
      pax,
      site,
      company,
      fullName,
      email,
      phoneNumber,
      bookingPolicy,
      memberId,
      notes,
    };
    return useNuxtApp().$api("/api/facilities-booking/v1", {
      method: "POST",
      body: data,
    });
  }

  /**
   * Reset calendar state
   */
  function resetCalendarState() {
    facilities.value = [];
    selectedFacility.value = "";
    isLoadingFacilities.value = false;
  }

  return {
    // State
    facilities,
    selectedFacility,
    isLoadingFacilities,

    // Methods
    getFacilitiesForCalendar,
    getBookingCalendar,
    createBooking,
    resetCalendarState,
  };
}
