// ─── Procedural Guardian Generators (Tiers 4–8) ─────────────────────────────────
// Extracted from guardian-encounters.ts to keep files under the 400-line limit.
//
//   Tier 4: Dual Element Pairs      (floors 250–280)
//   Tier 5: Dual Comp+Components    (floors 290–330)
//   Tier 6: Exotic+Components       (floors 340–380)
//   Tier 7: Exotic+Comp+Components  (floors 390–430)
//   Tier 8: Full Fusion             (floors 440+)

import type { GuardianDef, GuardianBoon } from '../types';
import { resolveMultiUnlockChain } from '../utils/guardian-utils';
import { generateGuardianName, getGuardianHP, blendColors } from './guardian-encounters';

// ─── Shared Helpers ─────────────────────────────────────────────────────────────

const COMPOSITE_ELEMENTS = ['metal', 'sand', 'lightning', 'frost', 'blackflame', 'radiantflames', 'miasma', 'shadowglass'];
const EXOTIC_ELEMENTS = ['crystal', 'stellar', 'void', 'soul', 'time', 'plasma'];

function makeBoons(elements: string[], floor: number): GuardianBoon[] {
  return elements.slice(0, 3).map((el) => ({
    type: 'elementalDamage' as const,
    value: 10 + Math.floor(floor / 20) * 5,
    desc: `+${10 + Math.floor(floor / 20) * 5}% ${el} damage`,
  }));
}

function defensiveStats(hpVal: number, baseMult: number, floorOffset: number, barrierMax: number, barrierBase: number) {
  const shield = Math.floor(hpVal * baseMult + floorOffset);
  const barrier = Math.min(barrierMax, barrierBase + floorOffset * 0.0002);
  return { shield, shieldRegen: Math.floor(shield * 0.02), barrier, barrierRegen: Math.floor(barrier * 0.01), healthRegen: Math.floor(hpVal * baseMult * 0.125), healthRegenIsPercent: false };
}

// ─── Tier 4: Dual Element Pairs (Floors 250–280) ────────────────────────────────

const DUAL_PAIRS: [string, string][] = [
  ['fire', 'water'], ['fire', 'air'], ['water', 'earth'], ['light', 'dark'],
  ['death', 'light'], ['fire', 'death'], ['water', 'dark'], ['air', 'light'], ['earth', 'death'],
];

export function getTier4Guardian(floor: number): GuardianDef {
  const [el1, el2] = DUAL_PAIRS[Math.floor((floor - 250) / 10) % DUAL_PAIRS.length];
  const hpVal = getGuardianHP(floor);
  const armor = Math.min(0.5, 0.30 + (floor - 250) * 0.003);
  const def = defensiveStats(hpVal, 0.04, (floor - 250) * 2, 0.15, 0.05);
  const pc = Math.floor(hpVal * 0.3 + hpVal * armor * 0.5 + def.shield * 2 + hpVal * def.barrier * 0.3);
  return {
    name: '', element: [el1, el2], hp: hpVal, pact: 7.5 + (floor - 250) * 0.05,
    color: blendColors(el1, el2), armor, ...def,
    boons: makeBoons([el1, el2], floor), pactCost: pc, pactTime: 20 + Math.floor((floor - 250) / 10),
    uniquePerk: `Dual-aspect: ${el1} and ${el2} spells gain +20% effectiveness`,
    power: Math.floor(hpVal * 0.5),
    effects: [{ type: `${el1}_boost`, value: 0.15 }, { type: `${el2}_boost`, value: 0.15 }],
    signingCost: { mana: pc, time: 20 + Math.floor((floor - 250) / 10) },
    unlocksMana: resolveMultiUnlockChain([el1, el2]),
    damageMultiplier: 3.5 + (floor - 250) * 0.02, insightMultiplier: 3.0 + (floor - 250) * 0.01,
  };
}

// ─── Tier 5: Dual Composite + Components (Floors 290–330) ───────────────────────

const DUAL_COMP_PAIRS: [string, string][] = [
  ['metal', 'sand'], ['metal', 'lightning'], ['sand', 'lightning'],
  ['frost', 'blackflame'], ['radiantflames', 'miasma'], ['shadowglass', 'frost'],
];

export function getTier5Guardian(floor: number): GuardianDef {
  const [comp1, comp2] = DUAL_COMP_PAIRS[Math.floor((floor - 290) / 10) % DUAL_COMP_PAIRS.length];
  const elements = resolveMultiUnlockChain([comp1, comp2]);
  const hpVal = getGuardianHP(floor);
  const armor = Math.min(0.55, 0.35 + (floor - 290) * 0.003);
  const def = defensiveStats(hpVal, 0.05, (floor - 290) * 2.5, 0.18, 0.07);
  const pc = Math.floor(hpVal * 0.3 + hpVal * armor * 0.5 + def.shield * 2 + hpVal * def.barrier * 0.3);
  return {
    name: '', element: elements, hp: hpVal, pact: 9.0 + (floor - 290) * 0.05,
    color: blendColors(comp1, comp2), armor, ...def,
    boons: makeBoons([comp1, comp2], floor), pactCost: pc, pactTime: 24 + Math.floor((floor - 290) / 10),
    uniquePerk: `Fusion twin-aspect: ${comp1} and ${comp2} spells gain +25% effectiveness`,
    power: Math.floor(hpVal * 0.55),
    effects: [{ type: 'armor_pierce', value: 0.2 }, { type: 'chain', value: 1 }],
    signingCost: { mana: pc, time: 24 + Math.floor((floor - 290) / 10) },
    unlocksMana: resolveMultiUnlockChain(elements),
    damageMultiplier: 4.0 + (floor - 290) * 0.02, insightMultiplier: 3.5 + (floor - 290) * 0.01,
  };
}

// ─── Tier 6: Exotic + Components (Floors 340–380) ───────────────────────────────

export function getTier6Guardian(floor: number): GuardianDef {
  const exoticEl = EXOTIC_ELEMENTS[Math.floor((floor - 340) / 10) % EXOTIC_ELEMENTS.length];
  const chain = resolveMultiUnlockChain([exoticEl]);
  const hpVal = getGuardianHP(floor);
  const armor = Math.min(0.6, 0.40 + (floor - 340) * 0.003);
  const def = defensiveStats(hpVal, 0.06, (floor - 340) * 3, 0.20, 0.08);
  const pc = Math.floor(hpVal * 0.35 + hpVal * armor * 0.5 + def.shield * 2 + hpVal * def.barrier * 0.3);
  return {
    name: '', element: chain, hp: hpVal, pact: 10.5 + (floor - 340) * 0.05,
    color: '#B8A9C9', armor, ...def,
    boons: [makeBoons([exoticEl], floor)[0]], pactCost: pc, pactTime: 28 + Math.floor((floor - 340) / 10),
    uniquePerk: `Exotic resonance: ${exoticEl} spells gain +30% effectiveness`,
    power: Math.floor(hpVal * 0.6),
    effects: [{ type: 'resist_ignore', value: 0.2 }, { type: 'reflect', value: 0.1 }],
    signingCost: { mana: pc, time: 28 + Math.floor((floor - 340) / 10) },
    unlocksMana: chain,
    damageMultiplier: 4.5 + (floor - 340) * 0.02, insightMultiplier: 4.0 + (floor - 340) * 0.01,
  };
}

// ─── Tier 7: Exotic + Composite + Components (Floors 390–430) ───────────────────

export function getTier7Guardian(floor: number): GuardianDef {
  const exoticEl = EXOTIC_ELEMENTS[Math.floor((floor - 390) / 10) % EXOTIC_ELEMENTS.length];
  const compEl = COMPOSITE_ELEMENTS[Math.floor((floor - 390) / 10) % COMPOSITE_ELEMENTS.length];
  const elements = resolveMultiUnlockChain([exoticEl, compEl]);
  const hpVal = getGuardianHP(floor);
  const armor = Math.min(0.65, 0.45 + (floor - 390) * 0.003);
  const def = defensiveStats(hpVal, 0.07, (floor - 390) * 3.5, 0.22, 0.10);
  const pc = Math.floor(hpVal * 0.4 + hpVal * armor * 0.5 + def.shield * 2 + hpVal * def.barrier * 0.3);
  return {
    name: '', element: elements, hp: hpVal, pact: 12.0 + (floor - 390) * 0.05,
    color: '#9B72AA', armor, ...def,
    boons: makeBoons([exoticEl, compEl], floor), pactCost: pc, pactTime: 32 + Math.floor((floor - 390) / 10),
    uniquePerk: `Primordial fusion: ${exoticEl} and ${compEl} spells gain +25% effectiveness`,
    power: Math.floor(hpVal * 0.65),
    effects: [{ type: 'resist_ignore', value: 0.25 }, { type: 'armor_pierce', value: 0.2 }, { type: 'chain', value: 1 }],
    signingCost: { mana: pc, time: 32 + Math.floor((floor - 390) / 10) },
    unlocksMana: elements,
    damageMultiplier: 5.0 + (floor - 390) * 0.02, insightMultiplier: 4.5 + (floor - 390) * 0.01,
  };
}

// ─── Tier 8: Full Fusion — 1 Exotic + 2 Composite + All Components (Floors 440+)

export function getTier8Guardian(floor: number): GuardianDef {
  const exoticEl = EXOTIC_ELEMENTS[Math.floor((floor - 440) / 10) % EXOTIC_ELEMENTS.length];
  const comp1 = COMPOSITE_ELEMENTS[Math.floor((floor - 440) / 10) % COMPOSITE_ELEMENTS.length];
  const comp2 = COMPOSITE_ELEMENTS[(Math.floor((floor - 440) / 10) + 1) % COMPOSITE_ELEMENTS.length];
  const elements = resolveMultiUnlockChain([exoticEl, comp1, comp2]);
  const hpVal = getGuardianHP(floor);
  const armor = Math.min(0.7, 0.50 + (floor - 440) * 0.002);
  const def = defensiveStats(hpVal, 0.08, (floor - 440) * 4, 0.25, 0.12);
  const pc = Math.floor(hpVal * 0.45 + hpVal * armor * 0.5 + def.shield * 2 + hpVal * def.barrier * 0.3);
  return {
    name: '', element: elements, hp: hpVal, pact: 14.0 + (floor - 440) * 0.05,
    color: '#7B5E9A', armor, ...def,
    boons: makeBoons([exoticEl, comp1, comp2], floor), pactCost: pc, pactTime: 36 + Math.floor((floor - 440) / 10),
    uniquePerk: `Cosmic convergence: All exotic, composite, and base spells gain +15% effectiveness`,
    power: Math.floor(hpVal * 0.7),
    effects: [{ type: 'resist_ignore', value: 0.3 }, { type: 'armor_pierce', value: 0.25 }, { type: 'chain', value: 2 }, { type: 'reflect', value: 0.1 }],
    signingCost: { mana: pc, time: 36 + Math.floor((floor - 440) / 10) },
    unlocksMana: elements,
    damageMultiplier: 5.5 + (floor - 440) * 0.02, insightMultiplier: 5.0 + (floor - 440) * 0.01,
  };
}

// ─── Procedural Guardian Lookup ─────────────────────────────────────────────────

export function getProceduralGuardian(floor: number): GuardianDef | null {
  if (floor < 250 || floor % 10 !== 0) return null;
  const tier = floor >= 440 ? 8 : floor >= 390 ? 7 : floor >= 340 ? 6 : floor >= 290 ? 5 : floor >= 250 ? 4 : 0;
  if (tier === 0) return null;
  const g = tier === 4 ? getTier4Guardian(floor) : tier === 5 ? getTier5Guardian(floor) : tier === 6 ? getTier6Guardian(floor) : tier === 7 ? getTier7Guardian(floor) : getTier8Guardian(floor);
  if (!g.name) {
    // Use only the primary (key) elements for name generation, not the full
    // component chain from resolveMultiUnlockChain(). This keeps names concise
    // (e.g. "Ignis-Aqua the Warden" instead of "Ignis-Petra-Marina the Warden").
    const compositesInChain = g.element.filter((e) => COMPOSITE_ELEMENTS.includes(e));
    const exoticsInChain = g.element.filter((e) => EXOTIC_ELEMENTS.includes(e));
    const keyElements = tier === 4
      ? g.element.slice(0, 2)
      : tier === 5
        ? compositesInChain.slice(0, 2)
        : tier === 6
          ? exoticsInChain.slice(0, 1)
          : tier === 7
            ? [...exoticsInChain.slice(0, 1), ...compositesInChain.slice(0, 1)]
            : [...exoticsInChain.slice(0, 1), ...compositesInChain.slice(0, 2)];
    g.name = generateGuardianName(keyElements, floor);
  }
  return g;
}
