import { describe, it, expect } from 'vitest';
import { getGolemSlots, isComponentUnlocked } from '@/lib/game/data/golems/utils';

// ─── Test: getGolemSlots ──────────────────────────────────────────────────────

describe('getGolemSlots', () => {
  it('returns 0 for fabricator level < 2', () => {
    expect(getGolemSlots(0)).toBe(0);
    expect(getGolemSlots(1)).toBe(0);
  });

  it('scales with fabricator level', () => {
    expect(getGolemSlots(2)).toBe(1);
    expect(getGolemSlots(3)).toBe(1);
    expect(getGolemSlots(4)).toBe(2);
    expect(getGolemSlots(5)).toBe(2);
    expect(getGolemSlots(6)).toBe(3);
    expect(getGolemSlots(7)).toBe(3);
    expect(getGolemSlots(8)).toBe(4);
    expect(getGolemSlots(9)).toBe(4);
    expect(getGolemSlots(10)).toBe(5);
  });

  it('returns base slots from fabricator level (cap applied at summon level)', () => {
    expect(getGolemSlots(10)).toBe(5);
    expect(getGolemSlots(11)).toBe(5);
    // getGolemSlots returns base slots only; total cap of 7 is applied in summon logic
    expect(getGolemSlots(20)).toBe(10);
  });

  it('base slots scale as floor(level/2)', () => {
    expect(getGolemSlots(0)).toBe(0);
    expect(getGolemSlots(1)).toBe(0);
    expect(getGolemSlots(2)).toBe(1);
    expect(getGolemSlots(10)).toBe(5);
  });
});

// ─── Test: isComponentUnlocked ────────────────────────────────────────────────

describe('isComponentUnlocked', () => {
  it('returns true for attunement_level requirement when met', () => {
    const req = { type: 'attunement_level' as const, attunement: 'fabricator', level: 2 };
    const attunements = { fabricator: { active: true, level: 2 } };
    expect(isComponentUnlocked(req, attunements, [], [])).toBe(true);
  });

  it('returns false for attunement_level when level is too low', () => {
    const req = { type: 'attunement_level' as const, attunement: 'fabricator', level: 2 };
    const attunements = { fabricator: { active: true, level: 1 } };
    expect(isComponentUnlocked(req, attunements, [], [])).toBe(false);
  });

  it('returns false for attunement_level when attunement is inactive', () => {
    const req = { type: 'attunement_level' as const, attunement: 'fabricator', level: 2 };
    const attunements = { fabricator: { active: false, level: 5 } };
    expect(isComponentUnlocked(req, attunements, [], [])).toBe(false);
  });

  it('returns true for mana_unlocked requirement when element is unlocked', () => {
    const req = { type: 'mana_unlocked' as const, manaType: 'frost' };
    expect(isComponentUnlocked(req, {}, ['earth', 'frost'], [])).toBe(true);
  });

  it('returns false for mana_unlocked when element is not unlocked', () => {
    const req = { type: 'mana_unlocked' as const, manaType: 'frost' };
    expect(isComponentUnlocked(req, {}, ['earth'], [])).toBe(false);
  });

  it('returns true for dual_attunement when both requirements met', () => {
    const req = {
      type: 'dual_attunement' as const,
      attunements: ['fabricator', 'enchanter'],
      levels: [4, 2],
    };
    const attunements = {
      fabricator: { active: true, level: 4 },
      enchanter: { active: true, level: 2 },
    };
    expect(isComponentUnlocked(req, attunements, [], [])).toBe(true);
  });

  it('returns false for dual_attunement when one attunement is too low', () => {
    const req = {
      type: 'dual_attunement' as const,
      attunements: ['fabricator', 'enchanter'],
      levels: [4, 2],
    };
    const attunements = {
      fabricator: { active: true, level: 4 },
      enchanter: { active: true, level: 1 },
    };
    expect(isComponentUnlocked(req, attunements, [], [])).toBe(false);
  });

  it('returns true for guardian_pact when attunements and pact are present', () => {
    const req = {
      type: 'guardian_pact' as const,
      attunements: ['invoker', 'fabricator'],
      levels: [5, 5],
    };
    const attunements = {
      invoker: { active: true, level: 5 },
      fabricator: { active: true, level: 5 },
    };
    expect(isComponentUnlocked(req, attunements, [], [1])).toBe(true);
  });

  it('returns false for guardian_pact when no pacts signed', () => {
    const req = {
      type: 'guardian_pact' as const,
      attunements: ['invoker', 'fabricator'],
      levels: [5, 5],
    };
    const attunements = {
      invoker: { active: true, level: 5 },
      fabricator: { active: true, level: 5 },
    };
    expect(isComponentUnlocked(req, attunements, [], [])).toBe(false);
  });

  it('returns false for unknown requirement type', () => {
    const req = { type: 'unknown_type' as any };
    expect(isComponentUnlocked(req, {}, [], [])).toBe(false);
  });
});
