import { DriverLeaderboardItemDTO } from '../types/generated/DriverLeaderboardItemDTO';
import { DriverLeaderboardItemViewModel } from './DriverLeaderboardItemViewModel';

export class DriverLeaderboardViewModel {
  drivers: DriverLeaderboardItemViewModel[];

  constructor(
    dto: { drivers: DriverLeaderboardItemDTO[] },
    previousDrivers?: DriverLeaderboardItemDTO[],
  ) {
    this.drivers = dto.drivers.map((driver, index) => {
      const previous = previousDrivers?.find(p => p.id === driver.id);
      return new DriverLeaderboardItemViewModel(driver, index + 1, previous?.rating);
    });
  }

  /** UI-specific: Total races across all drivers */
  get totalRaces(): number {
    return this.drivers.reduce((sum, driver) => sum + driver.racesCompleted, 0);
  }

  /** UI-specific: Total wins across all drivers */
  get totalWins(): number {
    return this.drivers.reduce((sum, driver) => sum + driver.wins, 0);
  }

  /** UI-specific: Active drivers count */
  get activeCount(): number {
    return this.drivers.filter(driver => driver.isActive).length;
  }
}
