import { describe, it, expect } from 'vitest';
import { pf2eApplyBoosts, parseAncestryBoosts, parseBackgroundBoosts, parseFlaw } from './abilities';

describe('pf2eApplyBoosts', () => {
  it('each boost adds +2 below 18', () => {
    // Human Fighter: ancestry 2 free (str, con), background (str, free dex), class str, 4 free (str, con, dex, wis)
    const scores = pf2eApplyBoosts(['str', 'con', 'str', 'dex', 'str', 'str', 'con', 'dex', 'wis']);
    expect(scores.str).toBe(18); // 10 +2+2+2+2 (four boosts) = 18
    expect(scores.con).toBe(14); // two boosts
    expect(scores.dex).toBe(14); // two boosts
    expect(scores.wis).toBe(12); // one boost
    expect(scores.int).toBe(10);
    expect(scores.cha).toBe(10);
  });

  it('applies a flaw before boosts', () => {
    const scores = pf2eApplyBoosts(['con'], 'cha');
    expect(scores.cha).toBe(8); // flaw -2
    expect(scores.con).toBe(12);
  });

  it('a 5th boost on one ability only adds +1 (already at 18)', () => {
    const scores = pf2eApplyBoosts(['str', 'str', 'str', 'str', 'str']);
    expect(scores.str).toBe(19); // 18 then +1
  });
});

describe('parseAncestryBoosts', () => {
  it('reads fixed boosts plus a Free token (Dwarf)', () => {
    expect(parseAncestryBoosts(['Constitution', 'Wisdom', 'Free'])).toEqual({ fixed: ['con', 'wis'], free: 1 });
  });
  it('reads "Two free ability boosts" (Human)', () => {
    expect(parseAncestryBoosts(['Two free ability boosts'])).toEqual({ fixed: [], free: 2 });
  });
  it('tolerates junk', () => {
    expect(parseAncestryBoosts(undefined)).toEqual({ fixed: [], free: 0 });
  });
});

describe('parseBackgroundBoosts', () => {
  it('lists the choose-one options and grants one free boost (Acolyte)', () => {
    expect(parseBackgroundBoosts(['Intelligence', 'Wisdom'])).toEqual({ options: ['int', 'wis'], free: 1 });
  });
});

describe('parseFlaw', () => {
  it('reads a flaw string or array', () => {
    expect(parseFlaw('Charisma')).toBe('cha');
    expect(parseFlaw(['Strength'])).toBe('str');
    expect(parseFlaw(undefined)).toBeUndefined();
  });
});
