import { withCDN } from "@/src/lib/utils";

/**
 * Hardcoded fallback assets keyed by the program slug (same slug values used
 * across the app, e.g. AllPrograms FALLBACK_PROGRAMS). Used when a selected
 * program has no logo / robot image of its own.
 */
export const SLUG_FALLBACK: Record<string, { logoImage: string; image: string }> = {
  ALGO_BUDDY: {
    logoImage: withCDN("/program-logo/logo_algo_buddy.svg"),
    image: withCDN("/Program-detail/algo-buddy/pct%20(2).png"),
  },
  ALGOC: {
    logoImage: withCDN("/program-logo/logo_algo_c.svg"),
    image: withCDN("/Program-detail/algoc/footer/Goal%20Hitter%201.png"),
  },
  BIG_BUILDERS: {
    logoImage: withCDN("/program-logo/logo_big_builders.svg"),
    image: withCDN("/Program-detail/Big-builders/footer/goose%201%20(1).png"),
  },
  BRICKS_CHALLENGE: {
    logoImage: withCDN("/program-logo/logo_bricks_chalenge.svg"),
    image: withCDN("/Program-detail/footer/accordion%20robot%201.png"),
  },
  ALGOPLAY: {
    logoImage: withCDN("/program-logo/logo_algo_play.svg"),
    image: withCDN("/Program-detail/algo-play/pct-5.png"),
  },
  GALILEO_TECHNIC: {
    logoImage: withCDN("/program-logo/logo_galileo_technic.svg"),
    image: withCDN("/Program-detail/galileo%20technic/footer/Steamroller%201.png"),
  },
  ROBO_TOYS: {
    logoImage: withCDN("/program-logo/logo_robo_toys.svg"),
    image: withCDN("/Program-detail/smartivo/zombie%201.png"),
  },
  SMARTIVO: {
    logoImage: withCDN("/program-logo/logo_smartivo.svg"),
    image: withCDN("/Program-detail/smartivo/Smartivo%20spotter_3%201.png"),
  },
};

// Normalize a slug so separator/case variants still match:
// "ALGO_BUDDY" / "algo-buddy" / "Algo Play" → "ALGOBUDDY" / "ALGOPLAY"
function normalize(slug: string): string {
  return slug.toString().toUpperCase().replace(/[^A-Z0-9]/g, "");
}

// Build a normalized lookup once so map keys stay human-readable above.
const NORMALIZED_FALLBACK: Record<string, { logoImage: string; image: string }> =
  Object.fromEntries(
    Object.entries(SLUG_FALLBACK).map(([key, value]) => [normalize(key), value]),
  );

/**
 * Resolve hardcoded fallback assets for a program slug (robust to case and
 * separators). Returns null when there's no known match.
 */
export function slugFallback(slug?: string) {
  if (!slug) return null;
  return NORMALIZED_FALLBACK[normalize(slug)] || null;
}
