import { describe, it, expect } from 'vitest';
import { cn } from '~/lib/utils';

function formatTime(seconds: number | undefined): string {
  if (seconds === undefined) return '--:--';
  const mins = Math.floor(seconds / 60);
  const secs = seconds % 60;
  return `${mins}:${secs.toString().padStart(2, '0')}`;
}

function isValidLeagueName(name: string): boolean {
  return name.trim().length >= 3 && name.trim().length <= 50;
}

type DraftSpeed = 'fast' | 'standard' | 'slow' | 'very-slow';

function getDraftTimes(speed: DraftSpeed): { initial: number; increment: number } {
  switch (speed) {
    case 'fast':
      return { initial: 60, increment: 10 };
    case 'standard':
      return { initial: 120, increment: 15 };
    case 'slow':
      return { initial: 28800, increment: 3600 };
    case 'very-slow':
      return { initial: 43200, increment: 3600 };
  }
}

describe('Utils', () => {
  describe('cn (className merger)', () => {
    it('should merge class names', () => {
      const result = cn('foo', 'bar');
      expect(result).toContain('foo');
      expect(result).toContain('bar');
    });

    it('should handle conditional classes', () => {
      const condition = false;
      const result = cn('foo', condition && 'bar', 'baz');
      expect(result).toContain('foo');
      expect(result).toContain('baz');
      expect(result).not.toContain('bar');
    });

    it('should handle undefined and null', () => {
      const result = cn('foo', undefined, null, 'bar');
      expect(result).toContain('foo');
      expect(result).toContain('bar');
    });

    it('should merge tailwind classes correctly', () => {
      const result = cn('px-2 py-1', 'px-4');
      // Should prefer the last px value
      expect(result).toBeDefined();
    });
  });
});

describe('Draft Time Formatting', () => {
  it('should format time correctly', () => {
    expect(formatTime(120)).toBe('2:00');
    expect(formatTime(90)).toBe('1:30');
    expect(formatTime(0)).toBe('0:00');
    expect(formatTime(3661)).toBe('61:01');
  });

  it('should handle undefined', () => {
    expect(formatTime(undefined)).toBe('--:--');
  });

  it('should pad seconds with zero', () => {
    expect(formatTime(65)).toBe('1:05');
    expect(formatTime(5)).toBe('0:05');
  });
});

describe('League Name Validation', () => {
  it('should accept valid league names', () => {
    expect(isValidLeagueName('My League')).toBe(true);
    expect(isValidLeagueName('Test League 2025')).toBe(true);
    expect(isValidLeagueName('ABC')).toBe(true);
  });

  it('should reject names that are too short', () => {
    expect(isValidLeagueName('ab')).toBe(false);
    expect(isValidLeagueName('a')).toBe(false);
    expect(isValidLeagueName('')).toBe(false);
  });

  it('should reject names that are too long', () => {
    const longName = 'a'.repeat(51);
    expect(isValidLeagueName(longName)).toBe(false);
  });

  it('should trim whitespace before validation', () => {
    expect(isValidLeagueName('  abc  ')).toBe(true);
    expect(isValidLeagueName('  ab  ')).toBe(false);
  });
});

describe('Draft Speed Presets', () => {
  it('should return correct times for fast speed', () => {
    const times = getDraftTimes('fast');
    expect(times.initial).toBe(60);
    expect(times.increment).toBe(10);
  });

  it('should return correct times for standard speed', () => {
    const times = getDraftTimes('standard');
    expect(times.initial).toBe(120);
    expect(times.increment).toBe(15);
  });

  it('should return correct times for slow speed', () => {
    const times = getDraftTimes('slow');
    expect(times.initial).toBe(28800); // 8 hours
    expect(times.increment).toBe(3600); // 1 hour
  });

  it('should return correct times for very-slow speed', () => {
    const times = getDraftTimes('very-slow');
    expect(times.initial).toBe(43200); // 12 hours
    expect(times.increment).toBe(3600); // 1 hour
  });
});
