"use client"
import { useRouter } from "next/navigation"
import { useFormContext } from "react-hook-form"

import FormContent from "./FormContent"
import { bookingWidgetVariants } from "./variants"

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

import type { BookingWidgetSchema } from "@/types/components/bookingWidget"
import type { BookingWidgetFormProps } from "@/types/components/form/bookingwidget"

const formId = "booking-widget"

export default function Form({ locations, type }: BookingWidgetFormProps) {
  const router = useRouter()

  const classNames = bookingWidgetVariants({
    type,
  })

  const { formState, handleSubmit, register } =
    useFormContext<BookingWidgetSchema>()

  function onSubmit(data: BookingWidgetSchema) {
    data.location = JSON.parse(decodeURIComponent(data.location))
    console.log(data)
    // TODO: Parse data and route accordignly to Select hotel or select room-rate page
    console.log("to be routing")
    router.push("/en/hotelreservation/select-hotel")
  }

  return (
    <section className={classNames}>
      <form
        onSubmit={handleSubmit(onSubmit)}
        className={styles.form}
        id={formId}
      >
        <input {...register("location")} type="hidden" />
        <FormContent
          locations={locations}
          formId={formId}
          formState={formState}
        />
      </form>
    </section>
  )
}
