"use client"
import { zodResolver } from "@hookform/resolvers/zod"
import { parsePhoneNumberFromString } from "libphonenumber-js"
import { useCallback, useEffect } from "react"
import { FormProvider, useForm } from "react-hook-form"
import { useIntl } from "react-intl"

import { useEnterDetailsStore } from "@/stores/enter-details"

import SpecialRequests from "@/components/HotelReservation/EnterDetails/Details/SpecialRequests"
import CountrySelect from "@/components/TempDesignSystem/Form/Country"
import Input from "@/components/TempDesignSystem/Form/Input"
import Phone from "@/components/TempDesignSystem/Form/Phone"
import Footnote from "@/components/TempDesignSystem/Text/Footnote"
import { useRoomContext } from "@/contexts/Details/Room"

import MemberPriceModal from "../MemberPriceModal"
import JoinScandicFriendsCard from "./JoinScandicFriendsCard"
import { guestDetailsSchema, signedInDetailsSchema } from "./schema"
import Signup from "./Signup"

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

import type {
  DetailsProps,
  DetailsSchema,
} from "@/types/components/hotelReservation/enterDetails/details"

const formID = "enter-details"
export default function Details({ user }: DetailsProps) {
  const intl = useIntl()

  const { addPreSubmitCallback } = useEnterDetailsStore((state) => ({
    addPreSubmitCallback: state.actions.addPreSubmitCallback,
  }))

  const {
    actions: { updateDetails, setIncomplete },
    room,
    roomNr,
    idx,
  } = useRoomContext()
  const initialData = room.guest

  const memberRate = "member" in room.roomRate ? room.roomRate.member : null

  const initialPhoneNumber = user?.phoneNumber || initialData.phoneNumber
  const parsedInitialPhoneNumber = initialPhoneNumber
    ? parsePhoneNumberFromString(initialPhoneNumber)
    : undefined
  let initialPhoneNumberCC = initialData.phoneNumberCC
  if (parsedInitialPhoneNumber && !initialPhoneNumberCC) {
    initialPhoneNumberCC = parsedInitialPhoneNumber.country ?? ""
  }
  const methods = useForm({
    defaultValues: {
      countryCode: user?.address?.countryCode || initialData.countryCode,
      dateOfBirth:
        "dateOfBirth" in initialData ? initialData.dateOfBirth : undefined,
      email: user?.email || initialData.email,
      firstName: user?.firstName || initialData.firstName,
      join: initialData.join,
      lastName: user?.lastName || initialData.lastName,
      membershipNo: initialData.membershipNo,
      phoneNumber: parsedInitialPhoneNumber?.isValid()
        ? parsedInitialPhoneNumber?.number
        : initialPhoneNumber,
      phoneNumberCC: initialPhoneNumberCC,
      zipCode: "zipCode" in initialData ? initialData.zipCode : undefined,
      specialRequest: {
        comment: room.specialRequest.comment,
      },
    },
    criteriaMode: "all",
    mode: "onBlur",
    resolver: zodResolver(user ? signedInDetailsSchema : guestDetailsSchema),
    reValidateMode: "onChange",
  })

  const {
    formState: { isValid },
    handleSubmit,
    trigger,
  } = methods

  useEffect(() => {
    addPreSubmitCallback(`${idx}-details`, trigger)
  }, [addPreSubmitCallback, idx, trigger])

  const onSubmit = useCallback(
    (values: DetailsSchema) => {
      updateDetails(values)
    },
    [updateDetails]
  )

  const updateDetailsStore = useCallback(() => {
    if (isValid) {
      handleSubmit(onSubmit)()
    } else {
      setIncomplete()
    }
  }, [handleSubmit, isValid, onSubmit, setIncomplete])

  useEffect(updateDetailsStore, [updateDetailsStore])

  return (
    <FormProvider {...methods}>
      <form
        className={styles.form}
        id={`${formID}-room-${roomNr}`}
        onSubmit={methods.handleSubmit(onSubmit)}
      >
        {user || !memberRate ? null : <JoinScandicFriendsCard />}
        <div className={styles.container}>
          <Footnote
            color="uiTextHighContrast"
            textTransform="uppercase"
            type="label"
            className={styles.fullWidth}
          >
            {intl.formatMessage({
              defaultMessage: "Guest information",
            })}
          </Footnote>
          <Input
            autoComplete="given-name"
            label={intl.formatMessage({
              defaultMessage: "First name",
            })}
            maxLength={30}
            name="firstName"
            readOnly={!!user}
            registerOptions={{ required: true, onBlur: updateDetailsStore }}
          />
          <Input
            autoComplete="family-name"
            label={intl.formatMessage({
              defaultMessage: "Last name",
            })}
            maxLength={30}
            name="lastName"
            readOnly={!!user}
            registerOptions={{ required: true, onBlur: updateDetailsStore }}
          />
          <CountrySelect
            className={styles.fullWidth}
            label={intl.formatMessage({
              defaultMessage: "Country",
            })}
            name="countryCode"
            readOnly={!!user}
            registerOptions={{ required: true, onBlur: updateDetailsStore }}
          />
          <Input
            autoComplete="email"
            className={styles.fullWidth}
            label={intl.formatMessage({
              defaultMessage: "Email address",
            })}
            name="email"
            readOnly={!!user}
            registerOptions={{ required: true, onBlur: updateDetailsStore }}
          />
          <Phone
            className={styles.fullWidth}
            label={intl.formatMessage({
              defaultMessage: "Phone number",
            })}
            name="phoneNumber"
            readOnly={!!user}
            registerOptions={{ required: true, onBlur: updateDetailsStore }}
          />
          {user ? null : (
            <div className={styles.fullWidth}>
              <Signup
                name="join"
                registerOptions={{ onBlur: updateDetailsStore }}
              />
            </div>
          )}
          <SpecialRequests registerOptions={{ onBlur: updateDetailsStore }} />
        </div>
        <MemberPriceModal />
      </form>
    </FormProvider>
  )
}
