"use client"
import { useIntl } from "react-intl"
import { useSearchParams } from "next/navigation"

import { alternativeHotels } from "@/constants/routes/hotelReservation"

import Alert from "@/components/TempDesignSystem/Alert"
import BookingCodeFilter from "@/components/HotelReservation/SelectHotel/BookingCodeFilter"
import { useRoomContext } from "@/contexts/Room"
import useLang from "@/hooks/useLang"

import RoomCard from "./RoomCard"
import RoomTypeFilter from "./RoomTypeFilter"

import styles from "./roomSelectionPanel.module.css"

import { AvailabilityEnum } from "@/types/components/hotelReservation/selectHotel/selectHotel"
import { AlertTypeEnum } from "@/types/enums/alert"
import { RateTypeEnum } from "@/types/enums/rateType"
import { useBookingCodeFilterStore } from "@/stores/bookingCode-filter"
import { BookingCodeFilterEnum } from "@/types/enums/bookingCodeFilter"

export default function RoomSelectionPanel() {
  const { rooms } = useRoomContext()
  const searchParams = useSearchParams()
  const bookingCode = searchParams.get("bookingCode")
  const intl = useIntl()
  const lang = useLang()
  const noAvailableRooms = rooms.every(
    (roomConfig) => roomConfig.status === AvailabilityEnum.NotAvailable
  )
  const activeCodeFilter = useBookingCodeFilterStore((state) => state.activeCodeFilter)

  let filteredRooms = rooms, isRegularRatesAvailableWithCode: boolean = false
  if (bookingCode) {
    isRegularRatesAvailableWithCode =
      !!bookingCode ?
        rooms?.some((room) => {
          return (
            room.status === "Available" &&
            room.products.some(
              (product) =>
                product.productType.public.rateType === RateTypeEnum.Regular
            )
          )
        })
        : false

    filteredRooms = !isRegularRatesAvailableWithCode || activeCodeFilter === BookingCodeFilterEnum.All
      ? rooms : rooms.filter((room) => {
        return room.products.every(
          (product) =>
            (activeCodeFilter === BookingCodeFilterEnum.Discounted &&
              product.productType.public.rateType !== RateTypeEnum.Regular) ||
            (activeCodeFilter === BookingCodeFilterEnum.Regular &&
              product.productType.public.rateType === RateTypeEnum.Regular)
        )
      })
  }

  return (
    <>
      {noAvailableRooms ? (
        <div className={styles.hotelAlert}>
          <Alert
            type={AlertTypeEnum.Info}
            heading={intl.formatMessage({ id: "No availability" })}
            text={intl.formatMessage({
              id: "There are no rooms available that match your request.",
            })}
            link={{
              title: intl.formatMessage({ id: "See alternative hotels" }),
              url: `${alternativeHotels(lang)}`,
              keepSearchParams: true,
            }}
          />
        </div>
      ) : null}
      <RoomTypeFilter />
      {bookingCode && isRegularRatesAvailableWithCode ? <BookingCodeFilter /> : null}
      <ul className={styles.roomList}>
        {filteredRooms.map((roomConfiguration) => (
          <RoomCard
            key={roomConfiguration.roomTypeCode}
            roomConfiguration={roomConfiguration}
          />
        ))}
      </ul>
    </>
  )
}
