// Type effectiveness helpers (Gen 6+ standard chart; includes Fairy)
// Used for Summary "Strong/Weak Against" lists.
//
// Notes on "combined" offensive summary for dual-types:
// - Strong Against: defender types where ANY of the Pokémon's own types is super-effective (2x).
// - Weak Against: defender types that resist ALL of the Pokémon's own types (0.5x or 0x).
// - Immune: defender types that are immune to ALL of the Pokémon's own types (0x).

const TYPES = [
  "normal","fire","water","electric","grass","ice","fighting","poison","ground","flying",
  "psychic","bug","rock","ghost","dragon","dark","steel","fairy"
];

// For each attacking type: which defending types take 2x, 0.5x, or 0x.
const OFFENSE = {
  normal:   { x2: [],                           x05: ["rock","steel"],                 x0: ["ghost"] },
  fire:     { x2: ["grass","ice","bug","steel"],x05: ["fire","water","rock","dragon"], x0: [] },
  water:    { x2: ["fire","ground","rock"],     x05: ["water","grass","dragon"],       x0: [] },
  electric: { x2: ["water","flying"],           x05: ["electric","grass","dragon"],    x0: ["ground"] },
  grass:    { x2: ["water","ground","rock"],    x05: ["fire","grass","poison","flying","bug","dragon","steel"], x0: [] },
  ice:      { x2: ["grass","ground","flying","dragon"], x05: ["fire","water","ice","steel"], x0: [] },
  fighting: { x2: ["normal","ice","rock","dark","steel"], x05: ["poison","flying","psychic","bug","fairy"], x0: ["ghost"] },
  poison:   { x2: ["grass","fairy"],            x05: ["poison","ground","rock","ghost"], x0: ["steel"] },
  ground:   { x2: ["fire","electric","poison","rock","steel"], x05: ["grass","bug"], x0: ["flying"] },
  flying:   { x2: ["grass","fighting","bug"],   x05: ["electric","rock","steel"],      x0: [] },
  psychic:  { x2: ["fighting","poison"],        x05: ["psychic","steel"],              x0: ["dark"] },
  bug:      { x2: ["grass","psychic","dark"],   x05: ["fire","fighting","poison","flying","ghost","steel","fairy"], x0: [] },
  rock:     { x2: ["fire","ice","flying","bug"],x05: ["fighting","ground","steel"],    x0: [] },
  ghost:    { x2: ["psychic","ghost"],          x05: ["dark"],                         x0: ["normal"] },
  dragon:   { x2: ["dragon"],                   x05: ["steel"],                        x0: ["fairy"] },
  dark:     { x2: ["psychic","ghost"],          x05: ["fighting","dark","fairy"],      x0: [] },
  steel:    { x2: ["ice","rock","fairy"],       x05: ["fire","water","electric","steel"], x0: [] },
  fairy:    { x2: ["fighting","dragon","dark"], x05: ["fire","poison","steel"],        x0: [] },
};

function titleCaseType(t) {
  if (!t) return "";
  return t.charAt(0).toUpperCase() + t.slice(1);
}

export function formatTyping(types) {
  if (!types || !Array.isArray(types) || types.length === 0) return "-";
  const cleaned = types.filter(Boolean).map(t => String(t).toLowerCase());
  if (cleaned.length === 0) return "-";
  return cleaned.map(titleCaseType).join(" / ");
}

function multiplier(attackType, defendType) {
  const a = OFFENSE[String(attackType).toLowerCase()];
  const d = String(defendType).toLowerCase();
  if (!a) return 1;
  if (a.x0.includes(d)) return 0;
  if (a.x2.includes(d)) return 2;
  if (a.x05.includes(d)) return 0.5;
  return 1;
}

function fmtList(arr) {
  if (!arr || arr.length === 0) return "-";
  return arr.map(titleCaseType).join(", ");
}

// Defensive summary (what hits this Pokémon). Kept for compatibility if other menus use it.
export function buildDefensiveSummary(defendingTypes) {
  const types = (Array.isArray(defendingTypes) ? defendingTypes : [])
    .filter(Boolean).map(t => String(t).toLowerCase());

  const weak = [];
  const resist = [];
  const immune = [];

  for (const atk of TYPES) {
    let m = 1;
    for (const def of types) {
      m *= multiplier(atk, def);
    }
    if (m === 0) immune.push(atk);
    else if (m > 1) weak.push(atk);
    else if (m < 1) resist.push(atk);
  }

  return {
    weakTo: fmtList(weak),
    strongAgainst: fmtList(resist), // "strong against" in defensive sense == resists
    immuneTo: fmtList(immune),
  };
}

// Offensive summary (what this Pokémon's OWN types hit well).
export function buildOffensiveSummary(attackingTypes) {
  const atks = (Array.isArray(attackingTypes) ? attackingTypes : [])
    .filter(Boolean).map(t => String(t).toLowerCase());

  if (atks.length === 0) {
    return { strongAgainst: "-", weakTo: "-", immuneTo: "-" };
  }

  const strong = [];
  const weak = [];
  const immune = [];

  for (const def of TYPES) {
    // Because the Pokémon can choose either of its STAB types:
    // - strong if ANY type is 2x
    // - immune only if ALL types are 0x
    // - weak if ALL types are <= 0.5x (and not immune)
    let anyStrong = false;
    let allImmune = true;
    let allResistOrImmune = true;

    for (const atk of atks) {
      const m = multiplier(atk, def);
      if (m === 2) anyStrong = true;
      if (m !== 0) allImmune = false;
      if (!(m === 0.5 || m === 0)) allResistOrImmune = false;
    }

    if (allImmune) {
      immune.push(def);
    } else if (anyStrong) {
      strong.push(def);
    } else if (allResistOrImmune) {
      weak.push(def);
    }
  }

  return {
    strongAgainst: fmtList(strong),
    weakTo: fmtList(weak),
    immuneTo: fmtList(immune),
  };
}
