/**
 * Site footer 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 type {
  FooterData,
  StrapiMedia,
} from "@/lib/strapi/types"

/* ------------------------------------------------------------------ */
/*  Bundled defaults                                                   */
/* ------------------------------------------------------------------ */

export const footerDefaults: FooterData = {
  logo: "/assets/MGM_Logo.png",
  description:
    "Building excellence across Entertainment, Hospitality, Manufacturing, and Logistics since 1963.",
  copyright: "© 2025 MGM Group. All rights reserved.",
  socialLinks: [
    { icon: "/assets/footer/mgm-social-fb.svg", url: "" },
    { icon: "/assets/footer/mgm-social-tw.svg", url: "" },
    { icon: "/assets/footer/mgm-social-in.svg", url: "" },
    { icon: "/assets/footer/mgm-social-ig.svg", url: "" },
  ],
  contactItems: [
    {
      icon: "/assets/footer/icon-location.svg",
      value: "MGM Group, Chennai, Tamil Nadu, India",
      href: "",
    },
    {
      icon: "/assets/footer/icon-phone.svg",
      value: "+91 898 989 8989",
      href: "tel:+918989898989",
    },
    {
      icon: "/assets/footer/icon-mail.svg",
      value: "info@mgmgroup.in",
      href: "mailto:info@mgmgroup.in",
    },
  ],
}

/* ------------------------------------------------------------------ */
/*  Helpers                                                           */
/* ------------------------------------------------------------------ */

type AnyRec = Record<string, unknown>

function str(val: unknown, fallback = ""): string {
  if (typeof val === "string") return val.trim() !== "" ? val : fallback
  if (typeof val === "number") return String(val)
  return fallback
}

function media(val: unknown): string {
  return getMediaUrl(val as StrapiMedia | null | undefined)
}

function withFallback<T>(value: T[] | undefined | null, fallback: T[]): T[] {
  return value && value.length > 0 ? value : fallback
}

/* ------------------------------------------------------------------ */
/*  Public API                                                        */
/* ------------------------------------------------------------------ */

export async function getFooterData(): Promise<FooterData> {
  const d = footerDefaults

  if (!isStrapiEnabled) return d

  const entry = await fetchSingle<AnyRec>("footer")
  if (!entry) return d

  const socialRows = (entry.socialLinks as AnyRec[] | undefined) ?? []
  const contactRows = (entry.contactItems as AnyRec[] | undefined) ?? []

  return {
    logo: media(entry.logo_1600x475) || d.logo,
    description: str(entry.description, d.description),
    copyright: str(entry.copyright, d.copyright),
    socialLinks: withFallback(
      socialRows.map((row, i) => {
        const fb = d.socialLinks[i] ?? d.socialLinks[0]
        return {
          icon: media(row.icon_1x1) || fb?.icon || "",
          url: str(row.url, fb?.url || ""),
        }
      }),
      d.socialLinks,
    ),
    contactItems: withFallback(
      contactRows.map((row, i) => {
        const fb = d.contactItems[i] ?? d.contactItems[0]
        return {
          icon: media(row.icon_1x1) || fb?.icon || "",
          value: str(row.value, fb?.value || ""),
          href: str(row.href, fb?.href || ""),
        }
      }),
      d.contactItems,
    ),
  }
}
