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

// ─── Test: SpireSummaryTab barrel export ───────────────────────────────────────

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

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

// ─── Test: Barrel export includes SpireSummaryTab ──────────────────────────────

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

// ─── Test: Guardian data ───────────────────────────────────────────────────────

describe('Guardian constants', () => {
  it('has 9 guardians defined', async () => {
    const { GUARDIANS } = await import('@/lib/game/constants');
    expect(Object.keys(GUARDIANS).length).toBe(9);
  });

  it('guardians are at expected floors', async () => {
    const { GUARDIANS } = await import('@/lib/game/constants');
    const expectedFloors = [10, 20, 30, 40, 50, 60, 80, 90, 100];
    for (const floor of expectedFloors) {
      expect(GUARDIANS[floor]).toBeDefined();
    }
  });

  it('all guardians have required fields', async () => {
    const { GUARDIANS } = await import('@/lib/game/constants');
    for (const [, def] of Object.entries(GUARDIANS)) {
      expect(def.name).toBeTruthy();
      expect(def.element).toBeTruthy();
      expect(def.hp).toBeGreaterThan(0);
      expect(def.color).toBeTruthy();
      expect(def.boons).toBeInstanceOf(Array);
      expect(def.boons.length).toBeGreaterThan(0);
    }
  });

  it('all guardians have unique elements', async () => {
    const { GUARDIANS } = await import('@/lib/game/constants');
    const elements = Object.values(GUARDIANS).map((g) => g.element);
    const uniqueElements = new Set(elements);
    expect(uniqueElements.size).toBe(elements.length);
  });
});

// ─── Test: Combat store spire fields ───────────────────────────────────────────

describe('Combat store spire state', () => {
  it('useCombatStore is importable', async () => {
    const mod = await import('@/lib/game/stores');
    expect(mod.useCombatStore).toBeDefined();
    expect(typeof mod.useCombatStore).toBe('function');
  });
});

// ─── Test: Floor element cycle ─────────────────────────────────────────────────

describe('Floor element cycle', () => {
  it('FLOOR_ELEM_CYCLE has 7 elements', async () => {
    const { FLOOR_ELEM_CYCLE } = await import('@/lib/game/constants');
    expect(FLOOR_ELEM_CYCLE.length).toBe(7);
  });

  it('element opposites define expected pairs', async () => {
    const { ELEMENT_OPPOSITES } = await import('@/lib/game/constants');
    // Core pairs are symmetric
    expect(ELEMENT_OPPOSITES['fire']).toBe('water');
    expect(ELEMENT_OPPOSITES['water']).toBe('fire');
    expect(ELEMENT_OPPOSITES['air']).toBe('earth');
    expect(ELEMENT_OPPOSITES['earth']).toBe('air');
    expect(ELEMENT_OPPOSITES['light']).toBe('dark');
    expect(ELEMENT_OPPOSITES['dark']).toBe('light');
    // Lightning has an asymmetric opposite (grounding)
    expect(ELEMENT_OPPOSITES['lightning']).toBe('earth');
  });
});

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

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