"use client"
import { useRouter, useSearchParams } from "next/navigation"

import RoomCard from "./RoomCard"

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

import { RoomSelectionProps } from "@/types/components/hotelReservation/selectRate/roomSelection"

export default function RoomSelection({
  roomConfigurations,
  roomCategories,
}: RoomSelectionProps) {
  const router = useRouter()
  const searchParams = useSearchParams()

  function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault()
    const queryParams = new URLSearchParams(searchParams)
    queryParams.set("roomClass", e.currentTarget.roomClass?.value)
    queryParams.set("flexibility", e.currentTarget.flexibility?.value)
    router.push(`select-bed?${queryParams}`)
  }

  return (
    <div className={styles.wrapper}>
      <form
        method="GET"
        action={`select-bed?${searchParams}`}
        onSubmit={handleSubmit}
      >
        <ul className={styles.roomList}>
          {roomConfigurations.roomConfigurations.map((roomConfiguration) => (
            <li key={roomConfiguration.roomType}>
              <RoomCard
                rateDefinitions={roomConfigurations.rateDefinitions}
                roomConfiguration={roomConfiguration}
                roomCategories={roomCategories}
              />
            </li>
          ))}
        </ul>
        {/*         <div className={styles.summary}>
          This is summary
          <Button type="submit" size="small" theme="primaryDark">
            {intl.formatMessage({ id: "Choose room" })}
          </Button>
        </div> */}
      </form>
    </div>
  )
}
