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 buildCorporatePageSeo(page, origin = '') {
  return buildResolvedPageSeo(page, origin, {
    ogImage: page?.heroImage || page?.featuredImage || page?.structuredContent?.heroImage || '',
  })
}

export function useCorporateContent() {
  const corporatePage = useResolvedPage('corporate')
  const { locale } = usePublicLocale()
  const structuredContent = computed(() => corporatePage.value?.structuredContent || {})
  const ui = computed(() => structuredContent.value.ui || {})
  const form = computed(() => structuredContent.value.form || {})

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

  const corporateUi = computed(() => ({
    comparisonHeading: ui.value.comparisonHeading || '',
    comparisonDescription: ui.value.comparisonDescription || '',
    benefitsHeading: ui.value.benefitsHeading || '',
  }))

  const corporateComparisonRows = computed(() => sortByOrder(structuredContent.value.comparisonRows || []))
  const corporateBenefits = computed(() => sortByOrder(structuredContent.value.benefits || []))
  const corporateServiceHighlights = computed(() => sortByOrder(structuredContent.value.serviceHighlights || []))
  const corporateStats = computed(() => sortByOrder(structuredContent.value.stats || []))

  const corporateForm = computed(() => ({
    title: form.value.title || '',
    description: form.value.description || '',
    successTitle: form.value.successTitle || '',
    successDescription: form.value.successDescription || '',
    successResetLabel: form.value.successResetLabel || '',
    submitLabel: form.value.submitLabel || '',
    submittingLabel: form.value.submittingLabel || '',
    selectPlaceholder: form.value.selectPlaceholder || '',
    privacyNotice: form.value.privacyNotice || '',
    phoneError: form.value.phoneError || '',
    labels: form.value.labels || {},
    placeholders: form.value.placeholders || {},
    fleetSizeOptions: form.value.fleetSizeOptions || [],
  }))

  return {
    corporatePage,
    corporateHero,
    corporateUi,
    corporateComparisonRows,
    corporateBenefits,
    corporateServiceHighlights,
    corporateStats,
    corporateForm,
  }
}
