/**
 * Careers page data layer.
 *
 * Same pattern as homepage & contact: bundled defaults with
 * 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 {
  CareersData,
  CareersDemographicStatData,
  CareersProcessStepData,
  CareersValueData,
  CareersWhyWorkCardData,
  StrapiMedia,
} from "@/lib/strapi/types"

/* ------------------------------------------------------------------ */
/*  Bundled defaults                                                   */
/* ------------------------------------------------------------------ */

export const careersDefaults: CareersData = {
  hero: {
    heading: "Careers at MGM",
    description:
      "Join a legacy of excellence and be part of a team that values integrity, innovation, and growth.",
    mobileImage: "/assets/careers/hero-bg-mobile.png",
    desktopImage: "/assets/careers/Career Hero banner.jpeg",
  },
  whyWorkUs: {
    badge: "Why Work With Us",
    badgeIcon: "/assets/careers/icon-sparkle.svg",
    cards: [
      {
        icon: "/assets/careers/icon-career-growth.svg",
        title: "Career Growth",
        description: " Continuous learning opportunities with clear paths for career advancement.",
      },
      {
        icon: "/assets/careers/icon-competitive-benefits.svg",
        title: "Competitive Benefits",
        description: "Comprehensive compensation packages with performance-based rewards.",
      },
      {
        icon: "/assets/careers/icon-diverse-culture.svg",
        title: "Diverse Culture",
        description: "Collaborate with talented professionals across varied industries.",
      },
      {
        icon: "/assets/careers/icon-work-life-balance.svg",
        title: "Work-Life Balance",
        description: " Flexible work options and strong focus on employee wellness and balance.",
      },
    ],
  },
  strongValues: {
    badge: "Built on Strong Values",
    badgeIcon: "/assets/careers/icon-sparkle.svg",
    description:
      "At MGM Group of Companies, we offer more than jobs—endless career opportunities across Hospitality, Transport, Logistics, Port Handling, Entertainment, Packaging, PET Manufacturing, IMFS and Distillery. If you're driven, send your resume to explore opportunities.",
    values: [
      {
        number: "01",
        title: "Hard Work",
        description: "Dedication and perseverance in every endeavor",
        icon: "/assets/careers/values/icon-hard-work.svg",
      },
      {
        number: "02",
        title: "Character",
        description: "Integrity and strong moral principles",
        icon: "/assets/careers/values/icon-character.svg",
      },
      {
        number: "03",
        title: "Integrity",
        description: "Honesty and ethical conduct in all dealings",
        icon: "/assets/careers/values/icon-integrity.svg",
      },
      {
        number: "04",
        title: "Truth",
        description: "Transparency and truthfulness in actions",
        icon: "/assets/careers/values/icon-truth.svg",
      },
      {
        number: "05",
        title: "Hope",
        description: "Optimism and faith in a better tomorrow",
        icon: "/assets/careers/values/icon-hope.svg",
      },
    ],
  },
  demographics: {
    badge: "Our Employee Demographics",
    badgeIcon: "/assets/careers/icon-sparkle.svg",
    description:
      "We celebrate a diverse workforce that reflects our core values. We seek passionate team members eager to drive positive change across our varied business sectors.",
    teamImage: "/assets/careers/demographics/team-photo-3.png",
    stats: [
      {
        icon: "/assets/careers/demographics/icon-employees-15.svg",
        value: "200+",
        label: "Number of Employees (15+ year)",
      },
      {
        icon: "/assets/careers/demographics/icon-employees-30.svg",
        value: "300+",
        label: "Number of Employees (30+ year)",
      },
      {
        icon: "/assets/careers/demographics/icon-avg-age.svg",
        value: "32+",
        label: "Avg Age Group",
      },
    ],
  },
  applicationProcess: {
    badge: "Application Process",
    badgeIcon: "/assets/careers/icon-sparkle.svg",
    steps: [
      {
        number: "01",
        title: "Apply Online",
        description: "Submit your application through our portal.",
      },
      {
        number: "02",
        title: "Initial Review",
        description: "Our team evaluates your qualifications.",
      },
      {
        number: "03",
        title: "Interview",
        description: "Meet our hiring managers for discussion.",
      },
      {
        number: "04",
        title: "Welcome Aboard",
        description: "Join the MGM family and begin your journey!",
      },
    ],
  },
  exploreJobs: {
    heading: "Explore Our Job Opportunities",
    ctaLabel: "view current openings",
    ctaHref: "/contact",
  },
}

/* ------------------------------------------------------------------ */
/*  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)
}

/* ------------------------------------------------------------------ */
/*  Public API                                                        */
/* ------------------------------------------------------------------ */

export async function getCareersData(): Promise<CareersData> {
  const d = careersDefaults

  if (!isStrapiEnabled) return d

  const entry = await fetchSingle<AnyRec>("careers")

  if (!entry) return d

  const hero = (entry.hero as AnyRec | undefined) ?? {}
  const whyWorkUs = (entry.whyWorkUs as AnyRec | undefined) ?? {}
  const strongValues = (entry.strongValues as AnyRec | undefined) ?? {}
  const demographics = (entry.demographics as AnyRec | undefined) ?? {}
  const applicationProcess = (entry.applicationProcess as AnyRec | undefined) ?? {}
  const exploreJobs = (entry.exploreJobs as AnyRec | undefined) ?? {}

  const cardRows = (whyWorkUs.cards as AnyRec[] | undefined) ?? []
  const valueRows = (strongValues.values as AnyRec[] | undefined) ?? []
  const statRows = (demographics.stats as AnyRec[] | undefined) ?? []
  const stepRows = (applicationProcess.steps as AnyRec[] | undefined) ?? []

  return {
    hero: {
      heading: str(hero.heading, d.hero.heading),
      description: str(hero.description, d.hero.description),
      mobileImage: media(hero.mobileImage_4x3) || d.hero.mobileImage,
      desktopImage: media(hero.desktopImage_3x1) || d.hero.desktopImage,
    },
    whyWorkUs: {
      badge: str(whyWorkUs.badge),
      badgeIcon: d.whyWorkUs.badgeIcon,
      cards: cardRows.map((row, i) => {
        return {
          icon: media(row.icon_5x4) || d.whyWorkUs.cards[i]?.icon || "",
          title: str(row.title, d.whyWorkUs.cards[i]?.title),
          description: str(row.description, d.whyWorkUs.cards[i]?.description),
        }
      }),
    },
    strongValues: {
      badge: str(strongValues.badge),
      badgeIcon: d.strongValues.badgeIcon,
      description: str(strongValues.description),
      values: valueRows.map((row, i) => {
        return {
          number: str(row.number, d.strongValues.values[i]?.number),
          title: str(row.title, d.strongValues.values[i]?.title),
          description: str(row.description, d.strongValues.values[i]?.description),
          icon: media(row.icon_1x1) || d.strongValues.values[i]?.icon || "",
        }
      }),
    },
    demographics: {
      badge: str(demographics.badge),
      badgeIcon: d.demographics.badgeIcon,
      description: str(demographics.description),
      teamImage: media(demographics.teamImage_2x1) || d.demographics.teamImage,
      stats: statRows.map((row, i) => {
        return {
          icon: media(row.icon_1x1) || d.demographics.stats[i]?.icon || "",
          value: str(row.value, d.demographics.stats[i]?.value),
          label: str(row.label, d.demographics.stats[i]?.label),
        }
      }),
    },
    applicationProcess: {
      badge: str(applicationProcess.badge),
      badgeIcon: d.applicationProcess.badgeIcon,
      steps: stepRows.map((row, i) => {
        return {
          number: str(row.number, d.applicationProcess.steps[i]?.number),
          title: str(row.title, d.applicationProcess.steps[i]?.title),
          description: str(row.description, d.applicationProcess.steps[i]?.description),
        }
      }),
    },
    exploreJobs: {
      heading: str(exploreJobs.heading, d.exploreJobs.heading),
      ctaLabel: str(exploreJobs.ctaButtonText, d.exploreJobs.ctaLabel),
      ctaHref: normalizeCmsLink(exploreJobs.ctaButtonLink, d.exploreJobs.ctaHref),
    },
  }
}
