<script setup>
import RequestTrackerView from '~/components/request/RequestTrackerView.vue'
import { buildRequestPageSeo, useRequestContent } from '~/content/request'
import { resolvePrivateFlowToken } from '~/utils/private-flow-route'
import { buildRequestRoute } from '~/utils/public-routes'
import { buildPublicSeoLinks } from '~/utils/public-seo'

const route = useRoute()
const { locale } = usePublicLocale()
const legacyToken = computed(() => String(route.query.token || '').trim())
const requestToken = computed(() => resolvePrivateFlowToken(route, '/request', 'token'))
const { requestPage } = useRequestContent(requestToken)

const requestPageSeo = computed(() => buildRequestPageSeo(requestPage.value, {
  privateFlow: Boolean(requestToken.value),
}))

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

useHead(() => ({
  link: buildPublicSeoLinks(requestPageSeo.value),
}))

if (legacyToken.value) {
  const query = { ...route.query }
  delete query.token

  await navigateTo(buildRequestRoute(legacyToken.value, query, {
    locale: locale.value,
  }), { replace: true })
}
</script>

<template>
  <RequestTrackerView :token="requestToken" />
</template>
