import { describe, it, expect } from 'vitest';
import { CORES, FRAMES, MIND_CIRCUITS, GOLEM_ENCHANTMENTS, ALL_CORES, ALL_FRAMES, ALL_MIND_CIRCUITS, ALL_GOLEM_ENCHANTMENTS } from '@/lib/game/data/golems';

// ─── Test: GolemancyTab barrel export ─────────────────────────────────────────

describe('GolemancyTab module structure', () => {
  it('exports GolemancyTab from barrel index', async () => {
    const mod = await import('../GolemancyTab');
    expect(mod.GolemancyTab).toBeDefined();
    expect(typeof mod.GolemancyTab).toBe('function');
  });

  it('GolemancyTab has correct displayName', async () => {
    const { GolemancyTab } = await import('../GolemancyTab');
    expect(GolemancyTab.displayName).toBe('GolemancyTab');
  });
});

// ─── Test: Barrel export includes GolemancyTab ────────────────────────────────

describe('Tab barrel export', () => {
  it('includes GolemancyTab in the tabs index', async () => {
    const mod = await import('@/components/game/tabs');
    expect(mod.GolemancyTab).toBeDefined();
    expect(typeof mod.GolemancyTab).toBe('function');
  });
});

// ─── Test: Component registries ───────────────────────────────────────────────

describe('Component registries', () => {
  it('CORES has all four core definitions', () => {
    expect(CORES.basic).toBeDefined();
    expect(CORES.intermediate).toBeDefined();
    expect(CORES.advanced).toBeDefined();
    expect(CORES.guardian).toBeDefined();
  });

  it('FRAMES has all seven frame definitions', () => {
    expect(FRAMES.earth).toBeDefined();
    expect(FRAMES.sand).toBeDefined();
    expect(FRAMES.frost).toBeDefined();
    expect(FRAMES.crystal).toBeDefined();
    expect(FRAMES.steel).toBeDefined();
    expect(FRAMES.shadowglass).toBeDefined();
    expect(FRAMES.crystalSteelHybrid).toBeDefined();
  });

  it('MIND_CIRCUITS has all four circuit definitions', () => {
    expect(MIND_CIRCUITS.simple).toBeDefined();
    expect(MIND_CIRCUITS.intermediate).toBeDefined();
    expect(MIND_CIRCUITS.advanced).toBeDefined();
    expect(MIND_CIRCUITS.guardian).toBeDefined();
  });

  it('GOLEM_ENCHANTMENTS has all eight enchantment definitions', () => {
    expect(GOLEM_ENCHANTMENTS.sword_fire).toBeDefined();
    expect(GOLEM_ENCHANTMENTS.sword_frost).toBeDefined();
    expect(GOLEM_ENCHANTMENTS.sword_lightning).toBeDefined();
    expect(GOLEM_ENCHANTMENTS.sword_shadow).toBeDefined();
    expect(GOLEM_ENCHANTMENTS.sword_metal).toBeDefined();
    expect(GOLEM_ENCHANTMENTS.sword_crystal).toBeDefined();
    expect(GOLEM_ENCHANTMENTS.sword_water).toBeDefined();
    expect(GOLEM_ENCHANTMENTS.sword_earth).toBeDefined();
  });

  it('ALL_CORES contains all core definitions', () => {
    expect(ALL_CORES.length).toBe(4);
  });

  it('ALL_FRAMES contains all frame definitions', () => {
    expect(ALL_FRAMES.length).toBe(7);
  });

  it('ALL_MIND_CIRCUITS contains all circuit definitions', () => {
    expect(ALL_MIND_CIRCUITS.length).toBe(4);
  });

  it('ALL_GOLEM_ENCHANTMENTS contains all enchantment definitions', () => {
    expect(ALL_GOLEM_ENCHANTMENTS.length).toBe(8);
  });

  it('all cores have required fields', () => {
    for (const core of ALL_CORES) {
      expect(core.id).toBeTruthy();
      expect(core.name).toBeTruthy();
      expect(core.description).toBeTruthy();
      expect(core.tier).toBeGreaterThanOrEqual(1);
      expect(core.tier).toBeLessThanOrEqual(4);
      expect(core.manaCapacity).toBeGreaterThan(0);
      expect(core.manaRegen).toBeGreaterThan(0);
      expect(core.maxRoomDuration).toBeGreaterThan(0);
      expect(core.summonCost.length).toBeGreaterThan(0);
      expect(core.primaryManaType).toBeTruthy();
      expect(core.tierMultiplier).toBeGreaterThan(0);
      expect(core.unlockRequirement).toBeDefined();
      expect(core.unlockRequirement.type).toBeTruthy();
    }
  });

  it('all frames have required fields', () => {
    for (const frame of ALL_FRAMES) {
      expect(frame.id).toBeTruthy();
      expect(frame.name).toBeTruthy();
      expect(frame.description).toBeTruthy();
      expect(frame.baseDamage).toBeGreaterThan(0);
      expect(frame.attackSpeed).toBeGreaterThan(0);
      expect(frame.armorPierce).toBeGreaterThanOrEqual(0);
      expect(frame.magicAffinity).toBeGreaterThanOrEqual(0);
      expect(frame.aoeTargets).toBeGreaterThanOrEqual(1);
      expect(frame.summonCost.length).toBeGreaterThan(0);
      expect(frame.specialEffect).toBeTruthy();
      expect(frame.unlockRequirement).toBeDefined();
    }
  });

  it('all mind circuits have required fields', () => {
    for (const circuit of ALL_MIND_CIRCUITS) {
      expect(circuit.id).toBeTruthy();
      expect(circuit.name).toBeTruthy();
      expect(circuit.description).toBeTruthy();
      expect(circuit.spellSlots).toBeGreaterThanOrEqual(0);
      expect(circuit.behavior).toBeTruthy();
      expect(circuit.summonCost.length).toBeGreaterThan(0);
      expect(circuit.unlockRequirement).toBeDefined();
    }
  });

  it('all enchantments have required fields', () => {
    for (const enchant of ALL_GOLEM_ENCHANTMENTS) {
      expect(enchant.id).toBeTruthy();
      expect(enchant.name).toBeTruthy();
      expect(enchant.description).toBeTruthy();
      expect(enchant.effect).toBeTruthy();
      expect(enchant.capacityCost).toBeGreaterThan(0);
      expect(enchant.summonCost.length).toBeGreaterThan(0);
    }
  });

  it('cores span four tiers', () => {
    const tiers = new Set(ALL_CORES.map(c => c.tier));
    expect(tiers.size).toBe(4);
    expect(tiers.has(1)).toBe(true);
    expect(tiers.has(2)).toBe(true);
    expect(tiers.has(3)).toBe(true);
    expect(tiers.has(4)).toBe(true);
  });

  it('basic core is the only tier 1 core', () => {
    const tier1 = ALL_CORES.filter(c => c.tier === 1);
    expect(tier1.length).toBe(1);
    expect(tier1[0].id).toBe('basic');
  });

  it('guardian core is the highest tier', () => {
    const guardian = CORES.guardian;
    expect(guardian).toBeDefined();
    expect(guardian.tier).toBe(4);
  });
});
