import { sumPackages } from "@/components/HotelReservation/utils"

import type { RoomState } from "@/types/stores/enter-details"

export function mapToPrice(rooms: RoomState[], isMember: boolean) {
  return rooms
    .filter((room) => room && room.room.roomRate)
    .map(({ room }, idx) => {
      const isMainRoom = idx === 0

      const pkgsSum = sumPackages(room.roomFeatures)

      if ("corporateCheque" in room.roomRate) {
        if (
          room.roomRate.corporateCheque.localPrice.additionalPricePerStay ||
          pkgsSum.price
        ) {
          return {
            ...room,
            packages: room.roomFeatures,
            price: {
              corporateCheque: {
                ...room.roomRate.corporateCheque.localPrice,
                additionalPricePerStay:
                  room.roomRate.corporateCheque.localPrice
                    .additionalPricePerStay || 0,
              },
            },
          }
        }
        return {
          ...room,
          packages: room.roomFeatures,
          price: {
            corporateCheque: room.roomRate.corporateCheque.localPrice,
          },
        }
      }

      if ("redemption" in room.roomRate) {
        if (
          room.roomRate.redemption.localPrice.additionalPricePerStay ||
          pkgsSum.price
        ) {
          return {
            ...room,
            packages: room.roomFeatures,
            price: {
              redemption: {
                ...room.roomRate.redemption.localPrice,
                additionalPricePerStay:
                  room.roomRate.redemption.localPrice.additionalPricePerStay ||
                  0,
              },
            },
          }
        }
        return {
          ...room,
          packages: room.roomFeatures,
          price: {
            redemption: room.roomRate.redemption.localPrice,
          },
        }
      }

      if ("voucher" in room.roomRate) {
        return {
          ...room,
          packages: room.roomFeatures,
          price: {
            voucher: room.roomRate.voucher,
          },
        }
      }

      const isMemberRate = !!(room.guest.join || room.guest.membershipNo)
      if ((isMember && isMainRoom) || isMemberRate) {
        if ("member" in room.roomRate && room.roomRate.member) {
          if (pkgsSum.price) {
            return {
              ...room,
              packages: room.roomFeatures,
              price: {
                regular: {
                  ...room.roomRate.member.localPrice,
                  pricePerNight: room.roomRate.member.localPrice.pricePerNight,
                  pricePerStay: room.roomRate.member.localPrice.pricePerStay,
                },
              },
            }
          }
          return {
            ...room,
            packages: room.roomFeatures,
            price: {
              regular: room.roomRate.member.localPrice,
            },
          }
        }
      }

      if ("public" in room.roomRate && room.roomRate.public) {
        if (pkgsSum.price) {
          return {
            ...room,
            packages: room.roomFeatures,
            price: {
              regular: {
                ...room.roomRate.public.localPrice,
                pricePerNight: room.roomRate.public.localPrice.pricePerNight,
                pricePerStay: room.roomRate.public.localPrice.pricePerStay,
              },
            },
          }
        }
        return {
          ...room,
          packages: room.roomFeatures,
          price: {
            regular: room.roomRate.public.localPrice,
          },
        }
      }

      console.error(room.roomRate)
      throw new Error(`Unknown roomRate`)
    })
}
