/**
 * Infrastructure Adapter: InMemoryLeaderboardsRepository
 *
 * In-memory implementation of LeaderboardsRepository.
 * Stores data in a Map structure.
 */

import {
  LeaderboardsRepository,
  LeaderboardDriverData,
  LeaderboardTeamData,
} from '../../../../core/leaderboards/application/ports/LeaderboardsRepository';

export class InMemoryLeaderboardsRepository implements LeaderboardsRepository {
  private drivers: Map<string, LeaderboardDriverData> = new Map();
  private teams: Map<string, LeaderboardTeamData> = new Map();

  async findAllDrivers(): Promise<LeaderboardDriverData[]> {
    return Array.from(this.drivers.values());
  }

  async findAllTeams(): Promise<LeaderboardTeamData[]> {
    return Array.from(this.teams.values());
  }

  async findDriversByTeamId(teamId: string): Promise<LeaderboardDriverData[]> {
    return Array.from(this.drivers.values()).filter(
      (driver) => driver.teamId === teamId,
    );
  }

  addDriver(driver: LeaderboardDriverData): void {
    this.drivers.set(driver.id, driver);
  }

  addTeam(team: LeaderboardTeamData): void {
    this.teams.set(team.id, team);
  }

  clear(): void {
    this.drivers.clear();
    this.teams.clear();
  }
}
