import {
  meetingsAndConferences,
  restaurantAndBar,
  wellnessAndExercise,
} from "@/constants/routes/hotelPageParams"

import { getLang } from "@/i18n/serverContext"

import {
  type Facilities,
  type FacilityCard,
  type FacilityCardType,
  FacilityCardTypeEnum,
  type FacilityGrid,
  type FacilityImage,
  RestaurantHeadings,
} from "@/types/components/hotelPage/facilities"
import { FacilityEnum } from "@/types/enums/facilities"
import type { Amenities, Facility } from "@/types/hotel"
import type { CardProps } from "@/components/TempDesignSystem/Card/card"

export function isFacilityCard(card: FacilityCardType): card is FacilityCard {
  return "heading" in card
}

export function isFacilityImage(card: FacilityCardType): card is FacilityImage {
  return "backgroundImage" in card
}

function setCardProps(
  theme: CardProps["theme"],
  heading: string,
  buttonText: string,
  href: string,
  scriptedTopTitle: string
): FacilityCard {
  return {
    theme,
    id: href,
    heading,
    scriptedTopTitle,
    secondaryButton: {
      href: `?s=${href}`,
      title: buttonText,
      isExternal: false,
    },
  }
}

export function setFacilityCardGrids(facilities: Facility[]): Facilities {
  const lang = getLang()

  const cards: Facilities = facilities.map((facility) => {
    let card: FacilityCard

    const grid: FacilityGrid = facility.heroImages.slice(0, 2).map((image) => {
      // Can be a maximum 2 images per grid
      const img: FacilityImage = {
        backgroundImage: {
          url: image.imageSizes.large,
          title: image.metaData.title,
          meta: {
            alt: image.metaData.altText,
            caption: image.metaData.altText_En,
          },
          id: image.imageSizes.large,
        },
        theme: "image",
        id: image.imageSizes.large,
      }
      return img
    })

    switch (facility.id) {
      case FacilityCardTypeEnum.wellness:
        card = setCardProps(
          "one",
          "Sauna and gym",
          "Read more about wellness & exercise",
          wellnessAndExercise[lang],
          facility.headingText
        )
        grid.unshift(card)
        break

      case FacilityCardTypeEnum.conference:
        card = setCardProps(
          "primaryDim",
          "Events that make an impression",
          "About meetings & conferences",
          meetingsAndConferences[lang],
          facility.headingText
        )
        grid.push(card)
        break

      case FacilityCardTypeEnum.restaurant:
        //const title = getRestaurantHeading(amenities) // TODO will be used later
        card = setCardProps(
          "primaryDark",
          "Enjoy relaxed restaurant experiences",
          "Read more & book a table",
          restaurantAndBar[lang],
          facility.headingText
        )
        grid.unshift(card)
        break
    }
    return grid
  })
  return cards
}

export function getRestaurantHeading(amenities: Amenities): RestaurantHeadings {
  const hasBar = amenities.some(
    (facility) =>
      facility.id === FacilityEnum.Bar ||
      facility.id === FacilityEnum.RooftopBar ||
      facility.id === FacilityEnum.Skybar
  )
  const hasRestaurant = amenities.some(
    (facility) => facility.id === FacilityEnum.Restaurant
  )

  if (hasBar && hasRestaurant) {
    return RestaurantHeadings.restaurantAndBar
  } else if (hasBar) {
    return RestaurantHeadings.bar
  } else if (hasRestaurant) {
    return RestaurantHeadings.restaurant
  }
  return RestaurantHeadings.breakfastRestaurant
}

export function filterFacilityCards(cards: FacilityGrid) {
  const card = cards.filter((card) => isFacilityCard(card))
  const images = cards.filter((card) => isFacilityImage(card))

  return {
    card: card[0] as FacilityCard,
    images: images as FacilityImage[],
  }
}
