import type { Price } from "@/types/components/hotelReservation/price"
import type {
  Rate,
  Room,
} from "@/types/components/hotelReservation/selectRate/selectRate"
import { CurrencyEnum } from "@/types/enums/currency"
import type { Packages } from "@/types/requests/packages"

export function mapRate(
  room: Rate,
  index: number,
  bookingRooms: Room[],
  packages: NonNullable<Packages>
) {
  const rate = {
    adults: bookingRooms[index].adults,
    cancellationText: room.product.rateDefinition?.cancellationText ?? "",
    childrenInRoom: bookingRooms[index].childrenInRoom ?? undefined,
    rateDetails: room.product.rateDefinition?.generalTerms,
    roomPrice: {
      currency: CurrencyEnum.Unknown,
      perNight: <Price>{
        local: {
          currency: CurrencyEnum.Unknown,
          price: 0,
        },
        requested: undefined,
      },
      perStay: <Price>{
        local: {
          currency: CurrencyEnum.Unknown,
          price: 0,
        },
        requested: undefined,
      },
    },
    roomRate: room.product,
    roomType: room.roomType,
    packages,
  }

  if ("corporateCheque" in room.product) {
    rate.roomPrice.currency = CurrencyEnum.CC
    rate.roomPrice.perNight.local = {
      currency: CurrencyEnum.CC,
      price: room.product.corporateCheque.localPrice.numberOfCheques,
      additionalPrice:
        room.product.corporateCheque.localPrice.additionalPricePerStay,
      additionalPriceCurrency:
        room.product.corporateCheque.localPrice.currency ??
        CurrencyEnum.Unknown,
    }
    rate.roomPrice.perStay.local = {
      currency: CurrencyEnum.CC,
      price: room.product.corporateCheque.localPrice.numberOfCheques,
      additionalPrice:
        room.product.corporateCheque.localPrice.additionalPricePerStay,
      additionalPriceCurrency:
        room.product.corporateCheque.localPrice.currency ??
        CurrencyEnum.Unknown,
    }
  } else if ("redemption" in room.product) {
    rate.roomPrice.currency = CurrencyEnum.POINTS
    rate.roomPrice.perNight.local = {
      currency: CurrencyEnum.POINTS,
      price: room.product.redemption.localPrice.pointsPerNight,
      additionalPrice:
        room.product.redemption.localPrice.additionalPricePerStay,
      additionalPriceCurrency:
        room.product.redemption.localPrice.currency ?? CurrencyEnum.Unknown,
    }
    rate.roomPrice.perStay.local = {
      currency: CurrencyEnum.POINTS,
      price: room.product.redemption.localPrice.pointsPerStay,
      additionalPrice:
        room.product.redemption.localPrice.additionalPricePerStay,
      additionalPriceCurrency:
        room.product.redemption.localPrice.currency ?? CurrencyEnum.Unknown,
    }
  } else if ("voucher" in room.product) {
    rate.roomPrice.currency = CurrencyEnum.Voucher
    rate.roomPrice.perNight.local = {
      currency: CurrencyEnum.Voucher,
      price: room.product.voucher.numberOfVouchers,
    }
    rate.roomPrice.perStay.local = {
      currency: CurrencyEnum.Voucher,
      price: room.product.voucher.numberOfVouchers,
    }
  } else {
    const currency =
      room.product.public?.localPrice.currency ||
      room.product.member?.localPrice.currency ||
      CurrencyEnum.Unknown
    rate.roomPrice.currency = currency
    rate.roomPrice.perNight.local = {
      currency,
      price:
        room.product.public?.localPrice.pricePerNight ||
        room.product.member?.localPrice.pricePerNight ||
        0,
    }
    rate.roomPrice.perStay.local = {
      currency,
      price:
        room.product.public?.localPrice.pricePerStay ||
        room.product.member?.localPrice.pricePerStay ||
        0,
    }
  }

  return rate
}
