"use client"
import { useIntl } from "react-intl"

import PointsRateCard from "@scandic-hotels/design-system/PointsRateCard"

import { sumPackages } from "@/components/HotelReservation/utils"
import { useRoomContext } from "@/contexts/SelectRate/Room"
import useRateTitles from "@/hooks/booking/useRateTitles"

import type { SharedRateCardProps } from "@/types/components/hotelReservation/selectRate/rates"
import { BookingCodeFilterEnum } from "@/types/enums/bookingCodeFilter"
import type { RedemptionProduct } from "@/types/trpc/routers/hotel/roomAvailability"

interface RedemptionsProps extends SharedRateCardProps {
  redemptions: RedemptionProduct[]
}

export default function Redemptions({
  handleSelectRate,
  redemptions,
  roomTypeCode,
}: RedemptionsProps) {
  const intl = useIntl()
  const rateTitles = useRateTitles()
  const { selectedFilter, selectedPackages, selectedRate } = useRoomContext()

  if (
    selectedFilter === BookingCodeFilterEnum.Discounted ||
    !redemptions.length
  ) {
    return null
  }

  const rewardNight = intl.formatMessage({
    defaultMessage: "Reward night",
  })
  const pkgsSum = sumPackages(selectedPackages)

  const breakfastIncluded = intl.formatMessage({
    defaultMessage: "Breakfast included",
  })
  const breakfastExcluded = intl.formatMessage({
    defaultMessage: "Breakfast excluded",
  })

  let selectedRateCode = ""
  if (selectedRate?.product && "redemption" in selectedRate.product) {
    selectedRateCode =
      selectedRate.roomTypeCode === roomTypeCode
        ? selectedRate.product.redemption.rateCode
        : ""
  }

  function handleSelect(rateCode: string) {
    const selectedRedemption = redemptions.find(
      (r) => r.redemption.rateCode === rateCode
    )
    if (selectedRedemption) {
      handleSelectRate(selectedRedemption)
    }
  }

  const rates = redemptions.map((r) => {
    let additionalPrice
    if (r.redemption.localPrice.additionalPricePerStay) {
      additionalPrice =
        r.redemption.localPrice.additionalPricePerStay + pkgsSum.price
    } else if (pkgsSum.price) {
      additionalPrice = pkgsSum.price
    }
    let additionalPriceCurrency
    if (r.redemption.localPrice.currency) {
      additionalPriceCurrency = r.redemption.localPrice.currency
    } else if (pkgsSum.currency) {
      additionalPriceCurrency = pkgsSum.currency
    }
    return {
      additionalPrice:
        additionalPrice && additionalPriceCurrency
          ? {
              currency: additionalPriceCurrency,
              price: additionalPrice.toString(),
            }
          : undefined,
      currency: "PTS",
      isDisabled: !r.redemption.hasEnoughPoints,
      points: r.redemption.localPrice.pointsPerStay.toString(),
      rateCode: r.redemption.rateCode,
    }
  })

  const notEnoughPoints = rates.every((rate) => rate.isDisabled)
  const firstRedemption = redemptions[0]
  const bannerText = firstRedemption.rateDefinition.breakfastIncluded
    ? `${rewardNight} ∙ ${breakfastIncluded}`
    : `${rewardNight} ∙ ${breakfastExcluded}`

  const rateTermDetails = [
    {
      title: rateTitles[firstRedemption.rate].title,
      terms: firstRedemption.rateDefinition.generalTerms,
    },
  ]

  return (
    <PointsRateCard
      key={firstRedemption.rate}
      bannerText={bannerText}
      onRateSelect={handleSelect}
      paymentTerm={rateTitles[firstRedemption.rate].paymentTerm}
      rates={rates}
      rateTitle={rateTitles[firstRedemption.rate].title}
      rateTermDetails={rateTermDetails}
      selectedRate={selectedRateCode}
      isNotEnoughPoints={notEnoughPoints}
      notEnoughPointsText={intl.formatMessage({
        defaultMessage: "Not enough points",
      })}
    />
  )
}
