import { describe, it, expect } from 'vitest';
import { normalizeOpen5eClass, normalizeFoundryClass } from './normalize';

describe('normalizeOpen5eClass', () => {
  it('parses hit die, saves, skill choices, caster and subclasses', () => {
    const c = normalizeOpen5eClass({
      name: 'Wizard', slug: 'wizard', desc: '### Arcane\n\nMasters of magic.',
      hit_dice: '1d6', prof_armor: 'None', prof_weapons: 'Daggers, darts, slings',
      prof_saving_throws: 'Intelligence, Wisdom',
      prof_skills: 'Choose two from Arcana, History, Insight, Investigation, Medicine, and Religion',
      spellcasting_ability: 'Intelligence',
      archetypes: [{ name: 'School of Evocation', desc: 'Boom.' }],
      document__title: 'Systems Reference Document', document__license_url: 'https://...',
    });
    expect(c.hitDie).toBe(6);
    expect(c.keyAbilities).toEqual(['int']);
    expect(c.saves).toEqual(['int', 'wis']);
    expect(c.skillCount).toBe(2);
    expect(c.skillChoices).toContain('Arcana');
    expect(c.caster).toBe('int');
    expect(c.subclasses[0]!.name).toBe('School of Evocation');
    expect(c.system).toBe('5e');
  });

  it('defaults caster to none when not a spellcaster', () => {
    const c = normalizeOpen5eClass({ name: 'Fighter', slug: 'fighter', hit_dice: '1d10', prof_saving_throws: 'Strength, Constitution', prof_skills: 'Choose two from Acrobatics, Athletics' });
    expect(c.caster).toBe('none');
    expect(c.keyAbilities).toEqual(['str']);
  });
});

describe('normalizeFoundryClass', () => {
  it('maps PF2e hp/key ability/saves/proficiencies to ranks', () => {
    const c = normalizeFoundryClass('fighter', {
      name: 'Fighter',
      system: {
        hp: 10, keyAbility: { value: ['dex', 'str'] }, perception: 2,
        savingThrows: { fortitude: 2, reflex: 2, will: 1 },
        trainedSkills: { value: [], additional: 3 },
        attacks: { simple: 2, martial: 2, advanced: 1, unarmed: 2, other: { name: '', rank: 0 } },
        defenses: { unarmored: 1, light: 1, medium: 1, heavy: 1 },
        description: { value: '<p>A master of arms.</p>' },
        publication: { title: 'Pathfinder Player Core', license: 'ORC' },
      },
    });
    expect(c.system).toBe('pf2e');
    expect(c.hitDie).toBe(10);
    expect(c.keyAbilities).toEqual(['dex', 'str']);
    expect(c.saveRanks).toEqual({ con: 'Expert', dex: 'Expert', wis: 'Trained' });
    expect(c.perception).toBe('Expert');
    expect(c.skillCount).toBe(3);
    expect(c.weapons).toEqual(expect.arrayContaining(['martial', 'simple']));
    expect(c.description).toBe('A master of arms.');
  });
});
