import { describe, it, expect } from 'vitest';
import {
  getAvailableCapacity,
  designFitsInEquipment,
  getEquipmentCategory,
  getEquipmentType,
} from '../crafting-utils';

function makeInstance(overrides = {}): any {
  return {
    instanceId: 'test_1',
    typeId: 'oakStaff',
    name: 'Test Staff',
    enchantments: [],
    totalCapacity: 100,
    usedCapacity: 0,
    ...overrides,
  };
}

function makeDesign(overrides = {}): any {
  return {
    id: 'design_1',
    name: 'Test Design',
    effects: [{ effectId: 'fireDamage', stacks: 2 }],
    totalCapacityUsed: 20,
    ...overrides,
  };
}

describe('getAvailableCapacity', () => {
  it('should return full capacity when nothing is used', () => {
    const instance = makeInstance({ totalCapacity: 100, usedCapacity: 0 });
    expect(getAvailableCapacity(instance)).toBe(100);
  });

  it('should return remaining capacity', () => {
    const instance = makeInstance({ totalCapacity: 100, usedCapacity: 30 });
    expect(getAvailableCapacity(instance)).toBe(70);
  });

  it('should return 0 when fully used', () => {
    const instance = makeInstance({ totalCapacity: 100, usedCapacity: 100 });
    expect(getAvailableCapacity(instance)).toBe(0);
  });

  it('should handle zero capacity', () => {
    const instance = makeInstance({ totalCapacity: 0, usedCapacity: 0 });
    expect(getAvailableCapacity(instance)).toBe(0);
  });
});

describe('designFitsInEquipment', () => {
  it('should return true when design fits', () => {
    const instance = makeInstance({ totalCapacity: 100, usedCapacity: 20 });
    const design = makeDesign({ totalCapacityUsed: 30 });
    expect(designFitsInEquipment(design, instance)).toBe(true);
  });

  it('should return false when design does not fit', () => {
    const instance = makeInstance({ totalCapacity: 100, usedCapacity: 80 });
    const design = makeDesign({ totalCapacityUsed: 30 });
    expect(designFitsInEquipment(design, instance)).toBe(false);
  });

  it('should return true when design fits exactly', () => {
    const instance = makeInstance({ totalCapacity: 100, usedCapacity: 70 });
    const design = makeDesign({ totalCapacityUsed: 30 });
    expect(designFitsInEquipment(design, instance)).toBe(true);
  });
});

describe('getEquipmentCategory', () => {
  it('should return the category for a known equipment type', () => {
    const cat = getEquipmentCategory('oakStaff');
    expect(cat).toBe('caster');
  });

  it('should return null for an unknown equipment type', () => {
    const cat = getEquipmentCategory('nonexistent_item');
    expect(cat).toBeNull();
  });

  it('should return accessory category for ring', () => {
    const cat = getEquipmentCategory('silverRing');
    expect(cat).toBe('accessory');
  });
});

describe('getEquipmentType', () => {
  it('should return the type definition for a known equipment type', () => {
    const type = getEquipmentType('oakStaff');
    expect(type).not.toBeNull();
    expect(type!.category).toBe('caster');
  });

  it('should return null for an unknown equipment type', () => {
    const type = getEquipmentType('nonexistent_item');
    expect(type).toBeNull();
  });
});