import type { Character, AttackLine } from "../types";
import { getModifier, formatModifier } from "./modifiers";
import {
  getEffectiveStat,
  getTalentAttackBonus,
  getTalentDamageBonus,
} from "./talent-effects";

export function generateAttacks(character: Character): AttackLine[] {
  const strMod = getModifier(getEffectiveStat(character, "STR"));
  const dexMod = getModifier(getEffectiveStat(character, "DEX"));
  const talentAttackBonus = getTalentAttackBonus(character);
  const talentDamageBonus = getTalentDamageBonus(character);

  const attacks: AttackLine[] = [];

  for (const gear of character.gear) {
    if (gear.type !== "weapon") continue;

    const effects = gear.effects;
    let damage = (effects.damage as string) || "1d4";
    const tags: string[] = [];

    let mod: number;
    if (effects.finesse) {
      mod = Math.max(strMod, dexMod);
      tags.push("F");
    } else if (effects.ranged) {
      mod = dexMod;
    } else {
      mod = strMod;
    }

    // Gear-specific bonus
    const gearBonus = (effects.bonus as number) || 0;
    mod += gearBonus + talentAttackBonus;

    // Talent damage bonus — append to damage expression
    if (talentDamageBonus > 0) {
      damage = `${damage}+${talentDamageBonus}`;
    }

    if (effects.two_handed) tags.push("2H");
    if (effects.thrown) tags.push("T");

    attacks.push({
      name: gear.name.toUpperCase(),
      modifier: mod,
      modifierStr: formatModifier(mod),
      damage,
      tags,
      isTalent: false,
    });
  }

  return attacks;
}
