import { computed } from 'vue'
import { useResolvedPage } from '~/content/pages'
import { buildResolvedPageSeo, sortByOrder } from '~/content/utils'
import { usePublicLocale } from '~/composables/usePublicLocale'
import { normalizePublicInternalPath } from '~/utils/public-routes'

export function buildPartnersPageSeo(page, origin = '') {
  return buildResolvedPageSeo(page, origin, {
    ogImage: page?.heroImage || page?.featuredImage || page?.structuredContent?.heroImage || '',
  })
}

export function usePartnersContent() {
  const partnersPage = useResolvedPage('partners')
  const { locale } = usePublicLocale()
  const structuredContent = computed(() => partnersPage.value?.structuredContent || {})
  const ui = computed(() => structuredContent.value.ui || {})
  const form = computed(() => structuredContent.value.form || {})

  const partnersHero = computed(() => ({
    badge: ui.value.heroBadge || partnersPage.value?.subtitle || '',
    title: partnersPage.value?.title || '',
    subtitle: partnersPage.value?.subtitle || '',
    description: partnersPage.value?.excerpt || '',
    image: structuredContent.value.heroImage || partnersPage.value?.heroImage || '',
    imageAlt: structuredContent.value.heroImageAlt || partnersPage.value?.title || '',
    ctaLabel: partnersPage.value?.cta?.label || '',
    ctaUrl: normalizePublicInternalPath(partnersPage.value?.cta?.url, locale.value) || '#apply',
  }))

  const partnersUi = computed(() => ({
    audiencesHeading: ui.value.audiencesHeading || '',
    audiencesDescription: ui.value.audiencesDescription || '',
    benefitsHeading: ui.value.benefitsHeading || '',
    benefitsDescription: ui.value.benefitsDescription || '',
    stepsHeading: ui.value.stepsHeading || '',
  }))

  const partnerAudiences = computed(() => sortByOrder(structuredContent.value.audiences || []))
  const partnerBenefits = computed(() => sortByOrder(structuredContent.value.benefits || []))
  const partnerSteps = computed(() => sortByOrder(structuredContent.value.steps || []))

  const partnerForm = computed(() => ({
    title: form.value.title || '',
    description: form.value.description || '',
    highlights: form.value.highlights || [],
    successTitle: form.value.successTitle || '',
    successDescription: form.value.successDescription || '',
    successResetLabel: form.value.successResetLabel || '',
    submitLabel: form.value.submitLabel || '',
    submittingLabel: form.value.submittingLabel || '',
    phoneError: form.value.phoneError || '',
    labels: form.value.labels || {},
    placeholders: form.value.placeholders || {},
    businessTypeOptions: form.value.businessTypeOptions || [],
  }))

  return {
    partnersPage,
    partnersHero,
    partnersUi,
    partnerAudiences,
    partnerBenefits,
    partnerSteps,
    partnerForm,
  }
}
