import type { Character } from "../types";

/**
 * Get the effective stat value after talent bonuses are applied.
 * e.g. if character has base STR 14 and a talent with { stat_bonus: 'STR', stat_bonus_value: 2 },
 * this returns 16.
 */
export function getEffectiveStat(
  character: Character,
  statName: string,
): number {
  const base =
    character.stats.find((s) => s.stat_name === statName)?.value ?? 10;
  let bonus = 0;
  for (const talent of character.talents) {
    if (talent.effect.stat_bonus === statName) {
      bonus += (talent.effect.stat_bonus_value as number) || 0;
    }
  }
  return base + bonus;
}

/**
 * Get total AC bonus from talents (e.g. Shield of Faith +1 AC).
 */
export function getTalentAcBonus(character: Character): number {
  let bonus = 0;
  for (const talent of character.talents) {
    if (typeof talent.effect.ac_bonus === "number") {
      bonus += talent.effect.ac_bonus;
    }
  }
  return bonus;
}

/**
 * Get total attack bonus from talents (e.g. Weapon Mastery +1).
 */
export function getTalentAttackBonus(character: Character): number {
  let bonus = 0;
  for (const talent of character.talents) {
    if (typeof talent.effect.attack_bonus === "number") {
      bonus += talent.effect.attack_bonus;
    }
  }
  return bonus;
}

/**
 * Get total damage bonus from talents (e.g. Weapon Mastery +1).
 */
export function getTalentDamageBonus(character: Character): number {
  let bonus = 0;
  for (const talent of character.talents) {
    if (typeof talent.effect.damage_bonus === "number") {
      bonus += talent.effect.damage_bonus;
    }
  }
  return bonus;
}

/**
 * Get extra gear slots from talents (e.g. Hauler +2).
 */
export function getTalentGearSlotsBonus(character: Character): number {
  let bonus = 0;
  for (const talent of character.talents) {
    if (typeof talent.effect.gear_slots_bonus === "number") {
      bonus += talent.effect.gear_slots_bonus;
    }
  }
  return bonus;
}

/**
 * Get HP bonus from talents (e.g. Grit +2 and +1 per level).
 */
export function getTalentHpBonus(character: Character): number {
  let bonus = 0;
  for (const talent of character.talents) {
    if (typeof talent.effect.hp_bonus === "number") {
      bonus += talent.effect.hp_bonus;
    }
    if (typeof talent.effect.hp_per_level === "number") {
      bonus += talent.effect.hp_per_level * character.level;
    }
  }
  return bonus;
}
