"use client"
import { useEffect, useState } from "react"
import { useIntl } from "react-intl"

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

import { CheckIcon, ChevronDownIcon } from "@/components/Icons"
import Button from "@/components/TempDesignSystem/Button"
import Footnote from "@/components/TempDesignSystem/Text/Footnote"
import Subtitle from "@/components/TempDesignSystem/Text/Subtitle"

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

import {
  StepEnum,
  StepStoreKeys,
} from "@/types/components/hotelReservation/enterDetails/step"
import { SectionAccordionProps } from "@/types/components/hotelReservation/selectRate/sectionAccordion"
import { BreakfastPackageEnum } from "@/types/enums/breakfast"

export default function SectionAccordion({
  header,
  label,
  step,
  children,
}: React.PropsWithChildren<SectionAccordionProps>) {
  const intl = useIntl()
  const currentStep = useEnterDetailsStore((state) => state.currentStep)
  const [isComplete, setIsComplete] = useState(false)
  const [isOpen, setIsOpen] = useState(false)
  const isValid = useEnterDetailsStore((state) => state.isValid[step])
  const navigate = useEnterDetailsStore((state) => state.navigate)
  const stepData = useEnterDetailsStore((state) => state.userData)
  const stepStoreKey = StepStoreKeys[step]
  const [title, setTitle] = useState(label)

  useEffect(() => {
    if (step === StepEnum.selectBed) {
      const value = stepData.bedType
      value && setTitle(value.description)
    }
    // If breakfast step, check if an option has been selected
    if (step === StepEnum.breakfast && stepData.breakfast) {
      const value = stepData.breakfast
      if (value === BreakfastPackageEnum.NO_BREAKFAST) {
        setTitle(intl.formatMessage({ id: "No breakfast" }))
      } else {
        setTitle(intl.formatMessage({ id: "Breakfast buffet" }))
      }
    }
  }, [stepData, stepStoreKey, step, intl])

  useEffect(() => {
    // We need to set the state on mount because of hydration errors
    setIsComplete(isValid)
  }, [isValid])

  useEffect(() => {
    setIsOpen(currentStep === step)
  }, [currentStep, step])

  function onModify() {
    navigate(step)
  }
  return (
    <section className={styles.wrapper} data-open={isOpen} data-step={step}>
      <div className={styles.iconWrapper}>
        <div className={styles.circle} data-checked={isComplete}>
          {isComplete ? (
            <CheckIcon color="white" height="16" width="16" />
          ) : null}
        </div>
      </div>
      <div className={styles.main}>
        <header className={styles.headerContainer}>
          <div>
            <Footnote
              asChild
              textTransform="uppercase"
              type="label"
              color="uiTextHighContrast"
            >
              <h2>{header}</h2>
            </Footnote>
            <Subtitle
              type="two"
              className={styles.selection}
              color="uiTextHighContrast"
            >
              {title}
            </Subtitle>
          </div>
          {isComplete && !isOpen && (
            <Button
              onClick={onModify}
              theme="base"
              size="small"
              variant="icon"
              intent="text"
              wrapping
            >
              {intl.formatMessage({ id: "Modify" })}{" "}
              <ChevronDownIcon color="burgundy" width={20} height={20} />
            </Button>
          )}
        </header>
        <div className={styles.content}>{children}</div>
      </div>
    </section>
  )
}
