"use client"
import { useSession } from "next-auth/react"
import { useIntl } from "react-intl"

import { useRatesStore } from "@/stores/select-rate"

import Caption from "@/components/TempDesignSystem/Text/Caption"
import { useRoomContext } from "@/contexts/SelectRate/Room"
import { isValidClientSession } from "@/utils/clientSession"

import { getBreakfastMessage } from "./getBreakfastMessage"

import { BookingCodeFilterEnum } from "@/types/enums/bookingCodeFilter"

export default function BreakfastMessage({
  breakfastIncludedMember,
  breakfastIncludedStandard,
  hasRegularRates,
}: {
  breakfastIncludedMember: boolean
  breakfastIncludedStandard: boolean
  hasRegularRates: boolean
}) {
  const intl = useIntl()
  const { roomNr, selectedFilter } = useRoomContext()

  const { hotelType } = useRatesStore((state) => ({
    hotelType: state.hotelType,
  }))
  const { data: session } = useSession()
  const isUserLoggedIn = isValidClientSession(session)

  const breakfastMessages = {
    included: intl.formatMessage({
      defaultMessage: "Breakfast is included.",
    }),
    notIncluded: intl.formatMessage({
      defaultMessage: "Breakfast excluded, add in next step.",
    }),
    noSelection: intl.formatMessage({
      defaultMessage: "Select a rate",
    }),
    scandicgo: intl.formatMessage({
      defaultMessage: "Breakfast deal can be purchased at the hotel.",
    }),
  }

  const breakfastMessage = getBreakfastMessage(
    breakfastIncludedStandard,
    breakfastIncludedMember,
    hotelType,
    isUserLoggedIn,
    breakfastMessages,
    roomNr
  )

  const isDiscount = selectedFilter === BookingCodeFilterEnum.Discounted

  if (isDiscount || !hasRegularRates) {
    return null
  }

  return (
    <span>
      <Caption color="uiTextHighContrast">{breakfastMessage}</Caption>
    </span>
  )
}
