import { describe, it, expect } from 'vitest';
import { DriverLeaderboardItemViewModel } from './DriverLeaderboardItemViewModel';
import { DriverLeaderboardItemDTO } from '../types/generated/DriverLeaderboardItemDTO';

describe('DriverLeaderboardItemViewModel', () => {
  const baseDto: DriverLeaderboardItemDTO & { avatarUrl: string } = {
    id: '1',
    name: 'Test Driver',
    rating: 1500,
    skillLevel: 'advanced',
    nationality: 'USA',
    racesCompleted: 50,
    wins: 10,
    podiums: 25,
    isActive: true,
    rank: 5,
    avatarUrl: 'https://example.com/avatar.jpg',
  };

  it('should create instance from DTO with avatar', () => {
    const viewModel = new DriverLeaderboardItemViewModel(baseDto, 1);

    expect(viewModel.id).toBe('1');
    expect(viewModel.name).toBe('Test Driver');
    expect(viewModel.position).toBe(1);
    expect(viewModel.avatarUrl).toBe('https://example.com/avatar.jpg');
  });

  it('should calculate win rate correctly', () => {
    const viewModel = new DriverLeaderboardItemViewModel(baseDto, 1);

    expect(viewModel.winRate).toBe(20); // 10/50 * 100
  });

  it('should format win rate as percentage', () => {
    const viewModel = new DriverLeaderboardItemViewModel(baseDto, 1);

    expect(viewModel.winRateFormatted).toBe('20.0%');
  });

  it('should return correct skill level color', () => {
    const viewModel = new DriverLeaderboardItemViewModel(baseDto, 1);

    expect(viewModel.skillLevelColor).toBe('orange'); // advanced = orange
  });

  it('should return correct skill level icon', () => {
    const viewModel = new DriverLeaderboardItemViewModel(baseDto, 1);

    expect(viewModel.skillLevelIcon).toBe('🥇'); // advanced = 🥇
  });

  it('should detect rating trend up', () => {
    const viewModel = new DriverLeaderboardItemViewModel(baseDto, 1, 1400);

    expect(viewModel.ratingTrend).toBe('up');
  });

  it('should detect rating trend down', () => {
    const viewModel = new DriverLeaderboardItemViewModel(baseDto, 1, 1600);

    expect(viewModel.ratingTrend).toBe('down');
  });

  it('should show rating change indicator', () => {
    const viewModel = new DriverLeaderboardItemViewModel(baseDto, 1, 1400);

    expect(viewModel.ratingChangeIndicator).toBe('+100');
  });

  it('should handle zero races for win rate', () => {
    const dto = { ...baseDto, racesCompleted: 0, wins: 0 };
    const viewModel = new DriverLeaderboardItemViewModel(dto, 1);

    expect(viewModel.winRate).toBe(0);
  });
});