/**
 * Site header data layer.
 *
 * Same pattern as the other pages: bundled defaults with a
 * Strapi merge-over-defaults when enabled.
 */

import { fetchSingle, getMediaUrl } from "@/lib/strapi/client"
import { isStrapiEnabled } from "@/lib/strapi/config"
import { normalizeCmsLink } from "@/lib/api/links"
import type { HeaderData, StrapiMedia } from "@/lib/strapi/types"

/* ------------------------------------------------------------------ */
/*  Bundled defaults                                                   */
/* ------------------------------------------------------------------ */

export const headerDefaults: HeaderData = {
  logo: "/assets/MGM_Logo.png",
  navLinks: [
    { label: 'About us', url: '/about' },
    { label: 'Our companies', url: '#companies', hasDropdown: true },
    { label: 'Career', url: '/careers' },
    { label: 'Newsletter', url: '/newsletter' }
  ],
  companyLinks: [
    { label: 'MGM SAFL', url: '/safl' },
    { label: 'MGM Muthu Hotels', url: '/hospitality' },
    { label: 'MGM Dizzee World', url: '/entertainment' },
    { label: 'MGM Vital Green Energy', url: '/green-energy' },
    { label: 'MGM Anand Transport', url: '/logistics' },
    { label: 'Vital Industries', url: '/vital-industries' },
    { label: 'MGM Weddings', url: '/wedding-collective' }
  ],
  mobileMenuLinks: [
    { label: 'Home', url: '/' },
    { label: 'About us', url: '/about' },
    { label: 'MGM SAFL', url: '/safl' },
    { label: 'MGM Dizzee World', url: '/entertainment' },
    { label: 'MGM Muthu Hotels', url: '/hospitality' },
    { label: 'MGM Vital Green Energy', url: '/green-energy' },
    { label: 'MGM Anand Transport', url: '/logistics' },
    { label: 'Vital Industries', url: '/vital-industries' },
    { label: 'Careers', url: '/careers' },
    { label: 'MGM Weddings', url: '/wedding-collective' },
    { label: 'Newsletter', url: '/newsletter' },
    { label: 'Contact us', url: '/contact' }
  ]
}

/* ------------------------------------------------------------------ */
/*  Helpers                                                           */
/* ------------------------------------------------------------------ */

type AnyRec = Record<string, unknown>

function media(val: unknown): string {
  return getMediaUrl(val as StrapiMedia | null | undefined)
}

function uniqueLinks<T extends { label?: string; url?: string }>(links: T[] | undefined): T[] | undefined {
  if (!links) return links
  const seen = new Set<string>()
  return links.filter((link) => {
    const key = `${String(link.label ?? "").trim().toLowerCase()}|${String(link.url ?? "").trim()}`
    if (seen.has(key)) return false
    seen.add(key)
    return true
  })
}

/* ------------------------------------------------------------------ */
/*  Public API                                                        */
/* ------------------------------------------------------------------ */

export async function getHeaderData(): Promise<HeaderData> {
  const d = headerDefaults

  if (!isStrapiEnabled) return d

  const entry = await fetchSingle<AnyRec>("header")
  if (!entry) return d

  const navLinks = (entry.navLinks as AnyRec[] | undefined)
  const companyLinks = (entry.companyLinks as AnyRec[] | undefined)
  const mobileMenuLinks = (entry.mobileMenuLinks as AnyRec[] | undefined)

  return {
    logo: media(entry.logo_1600x475) || d.logo,
    navLinks: navLinks ? uniqueLinks(navLinks.map(link => ({
      label: String(link.label),
      url: normalizeCmsLink(link.url, "/"),
      isBold: Boolean(link.isBold),
      hasDropdown: Boolean(link.hasDropdown)
    }))) : d.navLinks,
    companyLinks: companyLinks ? uniqueLinks(companyLinks.map(link => ({
      label: String(link.label),
      url: normalizeCmsLink(link.url, "#")
    }))) : d.companyLinks,
    mobileMenuLinks: mobileMenuLinks ? uniqueLinks(mobileMenuLinks.map(link => ({
      label: String(link.label),
      url: normalizeCmsLink(link.url, "/")
    }))) : d.mobileMenuLinks,
  }
}


