/**
 * Utility functions tests — cn, formatCurrency, formatPercent.
 *
 * These tests verify class name merging (with Tailwind conflict resolution),
 * currency formatting (USD locale), and percentage formatting.
 *
 * Coverage targets:
 *   - cn: merging, conditional classes, Tailwind conflict resolution
 *   - formatCurrency: whole numbers, decimals, zero, large numbers, negatives
 *   - formatPercent: whole percent, decimal percent, zero, rounding
 */

import { describe, it, expect } from 'vitest';
import { cn, formatCurrency, formatPercent } from '@/lib/utils';

describe('cn - class name merging', () => {
  it('merges multiple class names', () => {
    const result = cn('px-4', 'py-2', 'rounded');
    expect(result).toContain('px-4');
    expect(result).toContain('py-2');
    expect(result).toContain('rounded');
  });

  it('handles conditional classes (truthy/falsy)', () => {
    const isActive = true;
    const isDisabled = false;
    const result = cn(
      'base-class',
      isActive && 'active-class',
      isDisabled && 'disabled-class'
    );
    expect(result).toContain('base-class');
    expect(result).toContain('active-class');
    expect(result).not.toContain('disabled-class');
  });

  it('resolves Tailwind conflicts - padding', () => {
    // p-4 and p-2 both set padding; p-2 should win (last conflict wins)
    const result = cn('p-4', 'p-2');
    expect(result).toContain('p-2');
    expect(result).not.toContain('p-4');
  });

  it('resolves Tailwind conflicts - background color', () => {
    const result = cn('bg-red-500', 'bg-blue-600');
    expect(result).toContain('bg-blue-600');
    expect(result).not.toContain('bg-red-500');
  });

  it('resolves Tailwind conflicts - text color', () => {
    const result = cn('text-white', 'text-gray-700');
    expect(result).toContain('text-gray-700');
    expect(result).not.toContain('text-white');
  });

  it('handles empty inputs', () => {
    const result = cn('');
    expect(typeof result).toBe('string');
  });

  it('handles undefined and null values', () => {
    const result = cn('base', undefined, null, 'class');
    expect(result).toContain('base');
    expect(result).toContain('class');
  });
});

describe('formatCurrency - USD currency formatting', () => {
  it('formats whole numbers with two decimal places', () => {
    const result = formatCurrency(10);
    expect(result).toBe('$10.00');
  });

  it('formats decimal amounts', () => {
    const result = formatCurrency(9.99);
    expect(result).toBe('$9.99');
  });

  it('formats zero', () => {
    const result = formatCurrency(0);
    expect(result).toBe('$0.00');
  });

  it('formats large numbers with thousands separator', () => {
    const result = formatCurrency(1234.56);
    expect(result).toBe('$1,234.56');
  });

  it('formats very large numbers', () => {
    const result = formatCurrency(1000000);
    expect(result).toBe('$1,000,000.00');
  });

  it('formats negative numbers with minus sign', () => {
    const result = formatCurrency(-50.25);
    expect(result).toBe('-$50.25');
  });

  it('formats one cent', () => {
    const result = formatCurrency(0.01);
    expect(result).toBe('$0.01');
  });

  it('formats amounts with rounding (more than 2 decimals)', () => {
    const result = formatCurrency(9.999);
    // Intl.NumberFormat will round to 2 decimal places
    expect(result).toMatch(/\$\d+\.\d{2}/);
  });
});

describe('formatPercent - percentage formatting', () => {
  it('formats whole percent', () => {
    const result = formatPercent(50);
    expect(result).toBe('50%');
  });

  it('formats decimal percent with up to 2 decimal places', () => {
    const result = formatPercent(33.33);
    expect(result).toBe('33.33%');
  });

  it('formats zero percent', () => {
    const result = formatPercent(0);
    expect(result).toBe('0%');
  });

  it('formats one hundred percent', () => {
    const result = formatPercent(100);
    expect(result).toBe('100%');
  });

  it('rounds correctly - 33.336 rounds to 33.34%', () => {
    const result = formatPercent(33.336);
    expect(result).toBe('33.34%');
  });

  it('rounds correctly - 99.999 rounds to 100%', () => {
    const result = formatPercent(99.999);
    expect(result).toBe('100%');
  });

  it('formats small decimal percent', () => {
    const result = formatPercent(0.25);
    expect(result).toBe('0.25%');
  });

  it('handles negative percent (edge case)', () => {
    const result = formatPercent(-25);
    expect(result).toBe('-25%');
  });

  it('maintains precision for non-terminating decimals', () => {
    // Using a number that will have precision issues: 33.333...
    const result = formatPercent(100 / 3);
    expect(result).toMatch(/33\.33%/);
  });
});
