"use client"

import { useIntl } from "react-intl"

import { CheckIcon, InfoCircleIcon } from "@/components/Icons"
import Label from "@/components/TempDesignSystem/Form/Label"
import Popover from "@/components/TempDesignSystem/Popover"
import Caption from "@/components/TempDesignSystem/Text/Caption"

import PriceTable from "./PriceList"

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

import { FlexibilityOptionProps } from "@/types/components/hotelReservation/selectRate/flexibilityOption"

export default function FlexibilityOption({
  product,
  name,
  paymentTerm,
  priceInformation,
  roomType,
  roomTypeCode,
  features,
  petRoomPackage,
  handleSelectRate,
}: FlexibilityOptionProps) {
  const intl = useIntl()

  if (!product) {
    return (
      <div className={styles.noPricesCard}>
        <div className={styles.header}>
          <InfoCircleIcon width={16} height={16} color="uiTextMediumContrast" />
          <Caption>{name}</Caption>
          <Caption color="uiTextPlaceholder">({paymentTerm})</Caption>
        </div>
        <Label size="regular" className={styles.noPricesLabel}>
          <Caption color="uiTextHighContrast" type="bold">
            {intl.formatMessage({ id: "No prices available" })}
          </Caption>
        </Label>
      </div>
    )
  }

  const { public: publicPrice, member: memberPrice } = product.productType

  function onChange() {
    const rate = {
      roomTypeCode,
      roomType,
      priceName: name,
      public: publicPrice,
      member: memberPrice,
      features: petRoomPackage ? features : [],
    }
    handleSelectRate(rate)
  }

  return (
    <label>
      <input
        type="radio"
        name="rateCode"
        value={publicPrice?.rateCode}
        onChange={onChange}
      />
      <div className={styles.card}>
        <div className={styles.header}>
          <Popover
            placement="bottom left"
            triggerContent={
              <InfoCircleIcon
                width={16}
                height={16}
                color="uiTextMediumContrast"
              />
            }
          >
            <Caption
              color="uiTextHighContrast"
              type="bold"
              className={styles.popoverHeading}
            >
              {name}
            </Caption>
            {priceInformation?.map((info) => (
              <Caption
                key={info}
                color="uiTextHighContrast"
                className={styles.popoverText}
              >
                {info}
              </Caption>
            ))}
          </Popover>
          <Caption color="uiTextHighContrast">{name}</Caption>
          <Caption color="uiTextPlaceholder">({paymentTerm})</Caption>
        </div>
        <PriceTable
          publicPrice={publicPrice}
          memberPrice={memberPrice}
          petRoomPackage={petRoomPackage}
        />

        <div className={styles.checkIcon}>
          <CheckIcon color="white" height="16" width="16" />
        </div>
      </div>
    </label>
  )
}
