import { describe, it, expect } from 'vitest';

// ─── 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: Golem data integrity ───────────────────────────────────────────────

describe('Golem data', () => {
  it('all golems have required fields', async () => {
    const { GOLEMS_DEF } = await import('@/lib/game/data/golems');
    for (const [id, def] of Object.entries(GOLEMS_DEF)) {
      expect(def.id).toBe(id);
      expect(def.name).toBeTruthy();
      expect(def.description).toBeTruthy();
      expect(def.baseManaType).toBeTruthy();
      expect(def.summonCost.length).toBeGreaterThan(0);
      expect(def.maintenanceCost.length).toBeGreaterThan(0);
      expect(def.damage).toBeGreaterThan(0);
      expect(def.attackSpeed).toBeGreaterThan(0);
      expect(def.hp).toBeGreaterThan(0);
      expect(def.armorPierce).toBeGreaterThanOrEqual(0);
      expect(def.tier).toBeGreaterThanOrEqual(1);
      expect(def.unlockCondition).toBeTruthy();
    }
  });

  it('has golems across multiple tiers', async () => {
    const { GOLEMS_DEF } = await import('@/lib/game/data/golems');
    const tiers = new Set(Object.values(GOLEMS_DEF).map(g => g.tier));
    expect(tiers.size).toBeGreaterThanOrEqual(3);
  });

  it('earthGolem is the only base tier golem', async () => {
    const { GOLEMS_DEF } = await import('@/lib/game/data/golems');
    const baseGolems = Object.values(GOLEMS_DEF).filter(g => g.tier === 1);
    expect(baseGolems.length).toBe(1);
    expect(baseGolems[0].id).toBe('earthGolem');
  });

  it('voidstoneGolem is the highest tier', async () => {
    const { GOLEMS_DEF } = await import('@/lib/game/data/golems');
    const voidstone = GOLEMS_DEF.voidstoneGolem;
    expect(voidstone).toBeDefined();
    expect(voidstone.tier).toBe(4);
  });
});

// ─── Test: Golem utility functions ────────────────────────────────────────────

describe('Golem utility functions', () => {
  it('getGolemSlots returns 0 for fabricator level < 2', async () => {
    const { getGolemSlots } = await import('@/lib/game/data/golems');
    expect(getGolemSlots(0)).toBe(0);
    expect(getGolemSlots(1)).toBe(0);
  });

  it('getGolemSlots scales with fabricator level', async () => {
    const { getGolemSlots } = await import('@/lib/game/data/golems');
    expect(getGolemSlots(2)).toBe(1);
    expect(getGolemSlots(4)).toBe(2);
    expect(getGolemSlots(10)).toBe(5);
  });

  it('isGolemUnlocked returns false for unknown golem', async () => {
    const { isGolemUnlocked } = await import('@/lib/game/data/golems');
    expect(isGolemUnlocked('nonexistent', {}, [])).toBe(false);
  });

  it('isGolemUnlocked checks attunement level', async () => {
    const { isGolemUnlocked } = await import('@/lib/game/data/golems');
    expect(isGolemUnlocked('earthGolem', { fabricator: { active: true, level: 1 } }, [])).toBe(false);
    expect(isGolemUnlocked('earthGolem', { fabricator: { active: true, level: 2 } }, [])).toBe(true);
  });

  it('isGolemUnlocked checks mana unlocked', async () => {
    const { isGolemUnlocked } = await import('@/lib/game/data/golems');
    expect(isGolemUnlocked('steelGolem', {}, [])).toBe(false);
    expect(isGolemUnlocked('steelGolem', {}, ['metal'])).toBe(true);
  });

  it('canAffordGolemSummon returns false for unknown golem', async () => {
    const { canAffordGolemSummon } = await import('@/lib/game/data/golems');
    expect(canAffordGolemSummon('nonexistent', 100, {})).toBe(false);
  });

  it('canAffordGolemSummon checks raw mana cost', async () => {
    const { canAffordGolemSummon } = await import('@/lib/game/data/golems');
    // earthGolem costs 10 earth
    const elements = { earth: { current: 5, max: 100, unlocked: true } };
    expect(canAffordGolemSummon('earthGolem', 0, elements)).toBe(false);
    elements.earth.current = 10;
    expect(canAffordGolemSummon('earthGolem', 0, elements)).toBe(true);
  });
});

// ─── Test: Combat store golemancy state ───────────────────────────────────────

describe('Combat store golemancy', () => {
  it('toggleGolem is a function', async () => {
    const { useCombatStore } = await import('@/lib/game/stores/combatStore');
    const state = useCombatStore.getState();
    expect(typeof state.toggleGolem).toBe('function');
  });

  it('golemancy state has correct shape', async () => {
    const { useCombatStore } = await import('@/lib/game/stores/combatStore');
    const state = useCombatStore.getState();
    expect(state.golemancy).toBeDefined();
    expect(Array.isArray(state.golemancy.enabledGolems)).toBe(true);
    expect(Array.isArray(state.golemancy.summonedGolems)).toBe(true);
    expect(typeof state.golemancy.lastSummonFloor).toBe('number');
  });
});

// ─── Test: File size limit ────────────────────────────────────────────────────

describe('File size limits (400 lines max)', () => {
  it('GolemancyTab.tsx is under 400 lines', async () => {
    const fs = await import('fs');
    const path = await import('path');
    const filePath = path.join(__dirname, 'GolemancyTab.tsx');
    const content = fs.readFileSync(filePath, 'utf-8');
    const lines = content.split('\n').length;
    expect(lines).toBeLessThan(400);
  });
});
