import type { ImageSlot } from "./mpc-types";

export const retiredImageSlotAliases: Record<string, string> = {
  homepageHero: "img_heroes_homepage_hero",
  openNow: "img_local_options_practical_help",
  woodIceBraai: "img_local_options_wood_ice_supplies",
  foodDining: "img_local_options_food_dining",
  activities: "img_local_options_activities",
  leavingKruger: "img_route_packs_safe_exit_route_pack",
  advertise: "img_heroes_business_hero",
  marlothRiver: "img_heroes_plan_my_visit_hero",
  krugerGate: "img_heroes_free_kruger_help_hero",
  insideKruger: "img_route_packs_first_time_kruger_day_pack",
  onMyWay: "img_heroes_plan_my_visit_hero",
  img_slots_on_my_way: "img_heroes_plan_my_visit_hero",
  emergencyHelp: "img_local_options_practical_help",
  routePacks: "img_heroes_crocodile_bridge_morning_starter_pack_hero",
  guideChecklist: "img_heroes_free_guides_hero",
  businessRegister: "img_heroes_business_hero",
  supplierReport: "img_heroes_business_hero",
  supplierDefault: "img_fallbacks_default_business_listing",
  about: "img_heroes_about_hero",
  contact: "img_heroes_contact_hero",
  privacy: "img_heroes_legal_hero",
  shopHero: "img_slots_guide_checklist",
  freeGuides: "img_heroes_free_guides_hero",
  freeGuideLibrary: "img_heroes_free_guides_hero",
  img_heroes_free_guide_library_hero: "img_heroes_free_guides_hero",
  downloadsHero: "img_slots_route_packs",
  weekendSafari: "img_heroes_plan_my_visit_hero",
};

export const retiredImageSlotKeys = new Set(Object.keys(retiredImageSlotAliases));

const retiredImageSlotContentPatterns = [
  "founder safari vault",
  "founder_safari_vault",
  "founder-safari-vault",
  "founder safari",
  "founder_safari",
  "founder-safari",
  "safari vault",
  "safari_vault",
  "safari-vault",
  "img_heroes_founder",
  "img-heroes-founder",
  "/founder-safari",
  "/img-heroes-founder",
  "Free Guides",
];

function retiredSlotText(value: ImageSlot | string) {
  if (typeof value === "string") return value.toLowerCase();
  return `${value.key || ""} ${value.label || ""} ${value.description || ""} ${value.url || ""} ${value.fallbackUrl || ""} ${value.alt || ""}`.toLowerCase();
}

export function isDeprecatedImageSlot(slot: ImageSlot | string) {
  const text = retiredSlotText(slot);
  if (!text) return false;
  return retiredImageSlotContentPatterns.some((pattern) => text.includes(pattern));
}

export function canonicalImageSlotKey(key: string) {
  return retiredImageSlotAliases[key] || key;
}

export function isRetiredImageSlotKey(key: string) {
  return retiredImageSlotKeys.has(key) || isDeprecatedImageSlot(key);
}

export function isCustomImageSlotUrl(value?: string) {
  if (!value) return false;
  return value.startsWith("/uploads/") || value.startsWith("http://") || value.startsWith("https://");
}

export function filterVisibleImageSlots(slots: ImageSlot[]) {
  return slots.filter((slot) => !isRetiredImageSlotKey(slot.key) && !isDeprecatedImageSlot(slot));
}

export function migrateRetiredImageSlotUploads(baseSlots: ImageSlot[], parsedSlots: ImageSlot[]) {
  const byKey = new Map(parsedSlots.map((slot) => [slot.key, slot]));
  const baseByKey = new Map(baseSlots.map((slot) => [slot.key, slot]));

  for (const [retiredKey, canonicalKey] of Object.entries(retiredImageSlotAliases)) {
    const retiredSlot = byKey.get(retiredKey);
    if (!retiredSlot || !isCustomImageSlotUrl(retiredSlot.url)) continue;

    const existingCanonical = byKey.get(canonicalKey) || baseByKey.get(canonicalKey);
    if (existingCanonical && isCustomImageSlotUrl(existingCanonical.url)) continue;

    const baseCanonical = baseByKey.get(canonicalKey);
    byKey.set(canonicalKey, {
      ...(baseCanonical || retiredSlot),
      key: canonicalKey,
      label: baseCanonical?.label || retiredSlot.label,
      description: baseCanonical?.description || retiredSlot.description,
      url: retiredSlot.url,
      fallbackUrl: retiredSlot.fallbackUrl || retiredSlot.url,
      alt: retiredSlot.alt || baseCanonical?.alt || retiredSlot.label,
      updatedAt: retiredSlot.updatedAt,
    });
  }

  return byKey;
}
