// ─── Pact Utility Functions ───────────────────────────────────────────────────

import { getGuardianForFloor } from '../data/guardian-encounters';

export function computePactMultiplier(state: {
  signedPacts: number[];
  pactInterferenceMitigation?: number;
}): number {
  const { signedPacts, pactInterferenceMitigation = 0 } = state;

  if (signedPacts.length === 0) return 1.0;

  let baseMult = 1.0;
  for (const floor of signedPacts) {
    const guardian = getGuardianForFloor(floor);
    if (guardian) {
      baseMult *= guardian.damageMultiplier;
    }
  }

  if (signedPacts.length === 1) return baseMult;

  const numAdditionalPacts = signedPacts.length - 1;
  const basePenalty = 0.5 * numAdditionalPacts;
  const mitigationReduction = Math.min(pactInterferenceMitigation, 5) * 0.1;
  const effectivePenalty = Math.min(1.0, Math.max(0, basePenalty - mitigationReduction));

  if (pactInterferenceMitigation >= 5) {
    const synergyBonus = (pactInterferenceMitigation - 5) * 0.1;
    return baseMult * (1 + synergyBonus);
  }

  return baseMult * (1 - effectivePenalty);
}

export function computePactInsightMultiplier(state: {
  signedPacts: number[];
  pactInterferenceMitigation?: number;
}): number {
  const { signedPacts, pactInterferenceMitigation = 0 } = state;

  if (signedPacts.length === 0) return 1.0;

  let mult = 1.0;
  for (const floor of signedPacts) {
    const guardian = getGuardianForFloor(floor);
    if (guardian) {
      mult *= guardian.insightMultiplier;
    }
  }

  if (signedPacts.length > 1) {
    const numAdditionalPacts = signedPacts.length - 1;
    const basePenalty = 0.5 * numAdditionalPacts;
    const mitigationReduction = Math.min(pactInterferenceMitigation, 5) * 0.1;
    const effectivePenalty = Math.min(1.0, Math.max(0, basePenalty - mitigationReduction));

    if (pactInterferenceMitigation >= 5) {
      const synergyBonus = (pactInterferenceMitigation - 5) * 0.1;
      return mult * (1 + synergyBonus);
    }

    return mult * (1 - effectivePenalty);
  }

  return mult;
}
