import { type Lang } from "@/constants/languages"
import {
  getFiltersFromHotels,
  getSortedCities,
} from "@/stores/destination-data/helper"

import {
  getCityByCityIdentifier,
  getHotelIdsByCityIdentifier,
  getHotelIdsByCountry,
  getHotelsByHotelIds,
} from "../../../hotels/utils"
import { getCityPages } from "../../destinationCountryPage/utils"

import { ApiCountry } from "@/types/enums/country"
import { SortOption } from "@/types/enums/destinationFilterAndSort"
import type {
  MetadataInputSchema,
  RawMetadataSchema,
} from "@/types/trpc/routers/contentstack/metadata"

export const affix = "metadata"

export async function getCityData(
  data: RawMetadataSchema,
  input: MetadataInputSchema,
  serviceToken: string,
  lang: Lang
) {
  const destinationSettings = data.destination_settings
  const filter = input.filterFromUrl

  if (destinationSettings) {
    const {
      city_sweden,
      city_norway,
      city_denmark,
      city_finland,
      city_germany,
      city_poland,
    } = destinationSettings
    const cities = [
      city_denmark,
      city_finland,
      city_germany,
      city_poland,
      city_norway,
      city_sweden,
    ].filter((city): city is string => Boolean(city))

    const cityIdentifier = cities[0]

    if (cityIdentifier) {
      const cityData = await getCityByCityIdentifier({
        cityIdentifier,
        serviceToken,
        lang,
      })
      const hotelIds = await getHotelIdsByCityIdentifier(
        cityIdentifier,
        serviceToken
      )

      const hotels = await getHotelsByHotelIds({ hotelIds, lang, serviceToken })

      let filterType
      if (filter) {
        const allFilters = getFiltersFromHotels(hotels)
        const facilityFilter = allFilters.facilityFilters.find(
          (f) => f.slug === filter
        )
        const surroudingsFilter = allFilters.surroundingsFilters.find(
          (f) => f.slug === filter
        )

        if (facilityFilter) {
          filterType = "facility"
        } else if (surroudingsFilter) {
          filterType = "surroundings"
        }
      }

      return {
        location: cityData?.name,
        filter,
        filterType,
        hotelCount: hotelIds.length,
      }
    }
  }
  return null
}

export async function getCountryData(
  data: RawMetadataSchema,
  input: MetadataInputSchema,
  serviceToken: string,
  lang: Lang
) {
  const country = data.destination_settings?.country
  const filter = input.filterFromUrl

  if (country) {
    const translatedCountry = ApiCountry[lang][country]
    let filterType

    const cities = await getCityPages(lang, serviceToken, country)
    const sortedCities = getSortedCities(cities, SortOption.Recommended)
    const hotelIds = await getHotelIdsByCountry({
      country,
      serviceToken,
    })

    const hotels = await getHotelsByHotelIds({ hotelIds, lang, serviceToken })

    if (filter) {
      const allFilters = getFiltersFromHotels(hotels)
      const facilityFilter = allFilters.facilityFilters.find(
        (f) => f.slug === filter
      )
      const surroudingsFilter = allFilters.surroundingsFilters.find(
        (f) => f.slug === filter
      )

      if (facilityFilter) {
        filterType = "facility"
      } else if (surroudingsFilter) {
        filterType = "surroundings"
      }
    }
    return {
      location: translatedCountry,
      filter,
      filterType,
      cities: sortedCities.slice(0, 2).map(({ cityName }) => cityName),
      hotelCount: hotelIds.length,
    }
  }
  return null
}
