import { getIntl } from "@/i18n"

import type { RawMetadataSchema } from "@/types/trpc/routers/contentstack/metadata"

export async function getTitle(data: RawMetadataSchema) {
  const intl = await getIntl()
  const metadata = data.web?.seo_metadata
  if (metadata?.title) {
    return metadata.title
  }

  if (data.system.content_type_uid === "hotel_page" && data.hotelData) {
    if (data.subpageUrl) {
      const restaurantSubPage = data.hotelRestaurants?.find(
        (restaurant) => restaurant.nameInUrl === data.subpageUrl
      )
      if (restaurantSubPage) {
        const restaurantTitleLong = intl.formatMessage(
          {
            defaultMessage:
              "Explore {restaurantName} at {hotelName} in {destination}",
          },
          {
            restaurantName: restaurantSubPage.name,
            hotelName: data.hotelData.name,
            destination: data.hotelData.address.city,
          }
        )
        const restaurantTitleShort = intl.formatMessage(
          {
            defaultMessage: "Explore {restaurantName} at {hotelName}",
          },
          {
            restaurantName: restaurantSubPage.name,
            hotelName: data.hotelData.name,
          }
        )

        if (restaurantTitleLong.length > 60) {
          return restaurantTitleShort
        }
        return restaurantTitleLong
      }

      switch (data.subpageUrl) {
        case "reviews":
          const reviewsTitleLong = intl.formatMessage(
            {
              defaultMessage:
                "Ratings & reviews for {hotelName} in {destination}",
            },
            {
              hotelName: data.hotelData.name,
              destination: data.hotelData.address.city,
            }
          )
          const reviewsTitleShort = intl.formatMessage(
            { defaultMessage: "Ratings & reviews for {hotelName}" },
            { hotelName: data.hotelData.name }
          )
          if (reviewsTitleLong.length > 60) {
            return reviewsTitleShort
          }
          return reviewsTitleLong
        case data.additionalHotelData?.hotelParking.nameInUrl:
          const parkingTitleLong = intl.formatMessage(
            {
              defaultMessage:
                "Parking information for {hotelName} in {destination}",
            },
            {
              hotelName: data.hotelData.name,
              destination: data.hotelData.address.city,
            }
          )
          const parkingTitleShort = intl.formatMessage(
            { defaultMessage: "Parking information for {hotelName}" },
            { hotelName: data.hotelData.name }
          )

          if (parkingTitleLong.length > 60) {
            return parkingTitleShort
          }
          return parkingTitleLong
        case data.additionalHotelData?.healthAndFitness.nameInUrl:
          const wellnessTitleLong = intl.formatMessage(
            {
              defaultMessage:
                "Gym & health facilities at {hotelName} in {destination}",
            },
            {
              hotelName: data.hotelData.name,
              destination: data.hotelData.address.city,
            }
          )
          const wellnessTitleShort = intl.formatMessage(
            {
              defaultMessage: "Gym & health facilities at {hotelName}",
            },
            {
              hotelName: data.hotelData.name,
            }
          )

          if (wellnessTitleLong.length > 60) {
            return wellnessTitleShort
          }
          return wellnessTitleLong
        case data.additionalHotelData?.hotelSpecialNeeds.nameInUrl:
          const accessibilityTitleLong = intl.formatMessage(
            {
              defaultMessage:
                "Accessibility information for {hotelName} in {destination}",
            },
            {
              hotelName: data.hotelData.name,
              destination: data.hotelData.address.city,
            }
          )
          const accessibilityTitleShort = intl.formatMessage(
            {
              defaultMessage: "Accessibility information for {hotelName}",
            },
            {
              hotelName: data.hotelData.name,
            }
          )

          if (accessibilityTitleLong.length > 60) {
            return accessibilityTitleShort
          }
          return accessibilityTitleLong
        case data.additionalHotelData?.meetingRooms.nameInUrl:
          const meetingsTitleLong = intl.formatMessage(
            {
              defaultMessage:
                "Meetings & conferences at {hotelName} in {destination}",
            },
            {
              hotelName: data.hotelData.name,
              destination: data.hotelData.address.city,
            }
          )
          const meetingsTitleShort = intl.formatMessage(
            {
              defaultMessage: "Meetings & conferences at {hotelName}",
            },
            {
              hotelName: data.hotelData.name,
            }
          )

          if (meetingsTitleLong.length > 60) {
            return meetingsTitleShort
          }
          return meetingsTitleLong
        default:
          break
      }
    }
    return intl.formatMessage(
      {
        defaultMessage: "Stay at {hotelName} | Hotel in {destination}",
      },
      {
        hotelName: data.hotelData.name,
        destination: data.hotelData.address.city,
      }
    )
  }
  if (
    data.system.content_type_uid === "destination_city_page" ||
    data.system.content_type_uid === "destination_country_page"
  ) {
    if (data.destinationData) {
      const { location, filter, filterType } = data.destinationData
      if (location) {
        if (filter) {
          if (filterType === "facility") {
            return intl.formatMessage(
              {
                defaultMessage: "Hotels with {filter} in {location}",
              },
              { location, filter }
            )
          } else if (filterType === "surroundings") {
            return intl.formatMessage(
              {
                defaultMessage: "Hotels near {filter} in {location}",
              },
              { location, filter }
            )
          }
        }
        return intl.formatMessage(
          {
            defaultMessage: "Hotels in {location}",
          },
          { location }
        )
      }
    }
  }
  if (data.web?.breadcrumbs?.title) {
    return data.web.breadcrumbs.title
  }
  if (data.heading) {
    return data.heading
  }
  if (data.header?.heading) {
    return data.header.heading
  }
  return ""
}
