import { Typography } from "@scandic-hotels/design-system/Typography"

import { getIntl } from "@/i18n"

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

import {
  ExternalGymDetails,
  HealthFacilitiesEnum,
  OpeningHoursType,
} from "@/types/components/hotelPage/facilities"
import type { FacilityProps } from "@/types/components/hotelPage/sidepeek/facility"

interface OpeningHoursType extends FacilityProps {
  showTitle?: boolean
}

export default async function OpeningHours({
  data,
  showTitle = true,
}: OpeningHoursType) {
  const intl = await getIntl()

  const ordinaryOpeningTimes = data.openingDetails.openingHours.ordinary
  const weekendOpeningTimes = data.openingDetails.openingHours.weekends
  const shortDescription = data.content.texts.descriptions?.short

  const isExternalGym =
    data.type === HealthFacilitiesEnum.Gym &&
    data.details.find((d) => d.name === ExternalGymDetails.ExternalGym)
      ?.value === "True"

  const showOpeningHours =
    data.openingDetails.openingHoursType !== OpeningHoursType.SetFreeText &&
    !isExternalGym

  return (
    <>
      {shortDescription ? (
        <Typography variant="Body/Paragraph/mdRegular">
          <p>{shortDescription}</p>
        </Typography>
      ) : null}
      {showOpeningHours ? (
        <div className={styles.openingHoursContainer}>
          {showTitle ? (
            <Typography variant="Title/Subtitle/md">
              <h4>
                {intl.formatMessage({
                  defaultMessage: "Opening hours",
                })}
              </h4>
            </Typography>
          ) : null}
          <Typography variant="Body/Paragraph/mdRegular">
            <div className={styles.openingHours}>
              <p>
                {ordinaryOpeningTimes.alwaysOpen
                  ? intl.formatMessage({
                      defaultMessage: "Monday–Friday: Always open",
                    })
                  : intl.formatMessage(
                      {
                        defaultMessage:
                          "Monday–Friday: {openingTime}–{closingTime}",
                      },
                      {
                        openingTime: ordinaryOpeningTimes.openingTime,
                        closingTime: ordinaryOpeningTimes.closingTime,
                      }
                    )}
              </p>
              <p>
                {weekendOpeningTimes.alwaysOpen
                  ? intl.formatMessage({
                      defaultMessage: "Saturday–Sunday: Always open",
                    })
                  : intl.formatMessage(
                      {
                        defaultMessage:
                          "Saturday–Sunday: {openingTime}–{closingTime}",
                      },
                      {
                        openingTime: weekendOpeningTimes.openingTime,
                        closingTime: weekendOpeningTimes.closingTime,
                      }
                    )}
              </p>
            </div>
          </Typography>
        </div>
      ) : null}
    </>
  )
}
