<script setup>
import CityRentalPageRenderer from '~/components/city-rental/CityRentalPageRenderer.vue'
import GuidesIndexPage from '~/components/guides/GuidesIndexPage.vue'
import StandalonePageRenderer from '~/components/pages/StandalonePageRenderer.vue'
import PublicErrorScreen from '~/components/shared/PublicErrorScreen.vue'
import { buildPublicLocaleSwitchContext, useRegisterPublicLocaleSwitchContext } from '~/composables/usePublicLocaleSwitchContext'
import { usePublicLocale } from '~/composables/usePublicLocale'
import { buildCityRentalAutoRentalSchema, buildCityRentalFaqSchema } from '~/content/city-rental'
import { buildResolvedPageSeo } from '~/content/utils'
import { buildNotFoundPageSeo, useNotFoundContent } from '~/content/notFound'
import { resolvePageBySlug } from '~/content/pages'
import { normalizePublicLocale } from '~/utils/public-locales'
import { buildPublicSeoLinks } from '~/utils/public-seo'

definePageMeta({
  siteHeaderTheme: 'dark',
  siteHeaderOverlay: true,
})

const route = useRoute()
const event = useRequestEvent()
const { locale } = usePublicLocale()
const runtimeConfig = useRuntimeConfig()
const pageOrigin = computed(() => String(runtimeConfig.public.siteBaseUrl || '').replace(/\/+$/, ''))
const routePath = computed(() => route.path || '/')
const { data: matchedPage } = await useAsyncData(
  () => `catchall-page:${normalizePublicLocale(locale.value)}:${routePath.value}`,
  () => resolvePageBySlug(routePath.value, locale.value),
  {
    watch: [routePath, locale],
    default: () => null,
  },
)
const matchedContentKey = computed(() => String(matchedPage.value?.contentKey || '').trim())
const rendersGuidesIndex = computed(() => matchedContentKey.value === 'blog')
const rendersCityRentalPage = computed(() => String(matchedPage.value?.structuredContent?.ui?.template || '').trim() === 'city-rental')
const rendersStandalonePage = computed(() => Boolean(matchedPage.value) && !rendersGuidesIndex.value && !rendersCityRentalPage.value)
const { notFoundPage, notFoundUi } = useNotFoundContent({ registerLocaleSwitchContext: false })
const localeSwitchContext = computed(() => {
  if (!rendersStandalonePage.value && !rendersCityRentalPage.value) {
    return null
  }

  return buildPublicLocaleSwitchContext(matchedPage.value, { kind: 'list' })
})

useRegisterPublicLocaleSwitchContext(localeSwitchContext)

const pageSeo = computed(() => {
  if (rendersGuidesIndex.value) {
    return null
  }

  if (rendersCityRentalPage.value) {
    return buildResolvedPageSeo(matchedPage.value, pageOrigin.value, {
      ogImage: matchedPage.value?.heroImage || matchedPage.value?.featuredImage || '',
    })
  }

  if (rendersStandalonePage.value) {
    return buildResolvedPageSeo(matchedPage.value, pageOrigin.value, {
      ogImage: matchedPage.value?.heroImage || matchedPage.value?.featuredImage || '',
    })
  }

  return buildNotFoundPageSeo(notFoundPage.value)
})
const faqSchema = computed(() => {
  if (!rendersCityRentalPage.value) {
    return null
  }

  return buildCityRentalFaqSchema(matchedPage.value)
})
const rentalSchema = computed(() => {
  if (!rendersCityRentalPage.value) {
    return null
  }

  return buildCityRentalAutoRentalSchema(matchedPage.value)
})

useSeoMeta({
  title: () => pageSeo.value?.title,
  description: () => pageSeo.value?.description,
  ogTitle: () => pageSeo.value?.ogTitle,
  ogDescription: () => pageSeo.value?.ogDescription,
  robots: () => pageSeo.value?.robots,
})

useHead(() => ({
  link: pageSeo.value ? buildPublicSeoLinks(pageSeo.value) : [],
  script: rendersCityRentalPage.value ? [
    {
      type: 'application/ld+json',
      children: JSON.stringify(faqSchema.value),
    },
    {
      type: 'application/ld+json',
      children: JSON.stringify(rentalSchema.value),
    },
  ] : [],
}))

if (event && !rendersGuidesIndex.value && !rendersStandalonePage.value) {
  setResponseStatus(event, 404, 'Not Found')
}
</script>

<template>
  <GuidesIndexPage v-if="rendersGuidesIndex" />
  <CityRentalPageRenderer
    v-else-if="rendersCityRentalPage"
    :page="matchedPage"
  />
  <StandalonePageRenderer
    v-else-if="rendersStandalonePage"
    :page="matchedPage"
  />
  <PublicErrorScreen
    v-else
    :status-code="404"
    :title="notFoundUi.title || notFoundPage?.title"
    :description="notFoundUi.description || notFoundPage?.excerpt"
  />
</template>
