import type { IntlShape } from "react-intl"

import { HealthFacilitiesEnum } from "@/types/components/hotelPage/facilities"
import type {
  HotelPageSectionHeadings,
  HotelPageSections,
} from "@/types/components/hotelPage/sections"
import {
  TrackingChannelEnum,
  type TrackingSDKHotelInfo,
  type TrackingSDKPageData,
} from "@/types/components/tracking"
import { HotelHashValues } from "@/types/enums/hotelPage"
import type { Hotel, HotelData } from "@/types/hotel"
import type { HotelPage } from "@/types/trpc/routers/contentstack/hotelPage"
import type { Lang } from "@/constants/languages"

export function getRoomNameAsParam(roomName: string) {
  return roomName
    .replace(/\s?\(.*\)$/, "")
    .trim()
    .replace(/[()]/g, "")
    .replaceAll(" ", "-")
    .toLowerCase()
}

export function getTrackingPageData(
  pageData: HotelPage["hotel_page"]["system"],
  hotelData: Hotel,
  lang: Lang
) {
  const country = hotelData.address.country
  const city = hotelData.address.city
  const hotelName = hotelData.name

  const tracking: TrackingSDKPageData = {
    pageId: pageData.uid,
    domainLanguage: lang,
    publishDate: pageData.updated_at,
    createDate: pageData.created_at,
    channel: TrackingChannelEnum["hotels"],
    pageType: "hotelpage",
    pageName: `hotels|${country}|${city}|${hotelName}`,
    siteSections: `hotels|${country}|${city}|${hotelName}`,
    siteVersion: "new-web",
  }

  return tracking
}

export function getTrackingHotelData(hotelData: HotelData["hotel"]) {
  const tracking: TrackingSDKHotelInfo = {
    hotelID: hotelData.id,
  }

  return tracking
}

export function translateWellnessType(type: string, intl: IntlShape) {
  switch (type) {
    case HealthFacilitiesEnum.Gym:
      return intl.formatMessage({
        defaultMessage: "Gym",
      })
    case HealthFacilitiesEnum.IndoorPool:
      return intl.formatMessage({
        defaultMessage: "Indoor pool",
      })
    case HealthFacilitiesEnum.Jacuzzi:
      return intl.formatMessage({
        defaultMessage: "Jacuzzi",
      })
    case HealthFacilitiesEnum.OutdoorPool:
      return intl.formatMessage({
        defaultMessage: "Outdoor pool",
      })
    case HealthFacilitiesEnum.Relax:
      return intl.formatMessage({
        defaultMessage: "Relax",
      })
    case HealthFacilitiesEnum.Sauna:
      return intl.formatMessage({
        defaultMessage: "Sauna",
      })
    default:
      console.warn(`Unsupported group given: ${type}`)
      return intl.formatMessage({
        defaultMessage: "Wellness",
      })
  }
}

export function getPageSectionsData(
  intl: IntlShape,
  dynamicSections: {
    hasWellness: boolean
    hasRestaurants: boolean
    hasMeetingRooms: boolean
    hasActivities: boolean
    hasFAQ: boolean
  },
  sectionHeadings?: HotelPageSectionHeadings | null
) {
  const {
    hasWellness,
    hasRestaurants,
    hasMeetingRooms,
    hasActivities,
    hasFAQ,
  } = dynamicSections
  const sections: HotelPageSections = {
    overview: {
      hash: HotelHashValues.overview,
      heading:
        sectionHeadings?.overview ||
        intl.formatMessage({
          defaultMessage: "Overview",
        }),
    },
    rooms: {
      hash: HotelHashValues.rooms,
      heading:
        sectionHeadings?.rooms ||
        intl.formatMessage({
          defaultMessage: "Rooms",
        }),
    },
  }

  if (hasRestaurants) {
    sections.restaurant = {
      hash: HotelHashValues.restaurant,
      heading:
        sectionHeadings?.restaurant_bar ||
        intl.formatMessage({
          defaultMessage: "Restaurant & Bar",
        }),
    }
  }
  if (hasMeetingRooms) {
    sections.meetings = {
      hash: HotelHashValues.meetings,
      heading:
        sectionHeadings?.conferences_meetings ||
        intl.formatMessage({
          defaultMessage: "Meetings & conferences",
        }),
    }
  }
  if (hasWellness) {
    sections.wellness = {
      hash: HotelHashValues.wellness,
      heading:
        sectionHeadings?.health_wellness ||
        intl.formatMessage({
          defaultMessage: "Gym & Wellness",
        }),
    }
  }
  if (hasActivities) {
    sections.activities = {
      hash: HotelHashValues.activities,
      heading:
        sectionHeadings?.activities ||
        intl.formatMessage({
          defaultMessage: "Activities",
        }),
    }
  }
  if (hasFAQ) {
    sections.faq = {
      hash: HotelHashValues.faq,
      heading:
        sectionHeadings?.faq ||
        intl.formatMessage({
          defaultMessage: "FAQ",
        }),
    }
  }

  return sections
}
