/**
 * Infrastructure Adapter: InMemoryDriverStatsRepository
 * 
 * In-memory implementation of IDriverStatsRepository.
 * Stores computed driver statistics for caching and frontend queries.
 */

import type { IDriverStatsRepository } from '@core/racing/domain/repositories/IDriverStatsRepository';
import type { DriverStats } from '@core/racing/application/use-cases/IDriverStatsUseCase';
import type { Logger } from '@core/shared/application';

export class InMemoryDriverStatsRepository implements IDriverStatsRepository {
  private stats = new Map<string, DriverStats>();

  constructor(private readonly logger: Logger) {
    this.logger.info('[InMemoryDriverStatsRepository] Initialized.');
  }

  async getDriverStats(driverId: string): Promise<DriverStats | null> {
    this.logger.debug(`[InMemoryDriverStatsRepository] Getting stats for driver: ${driverId}`);
    return this.stats.get(driverId) ?? null;
  }

  getDriverStatsSync(driverId: string): DriverStats | null {
    this.logger.debug(`[InMemoryDriverStatsRepository] Getting stats (sync) for driver: ${driverId}`);
    return this.stats.get(driverId) ?? null;
  }

  async saveDriverStats(driverId: string, stats: DriverStats): Promise<void> {
    this.logger.debug(`[InMemoryDriverStatsRepository] Saving stats for driver: ${driverId}`);
    this.stats.set(driverId, stats);
  }

  async getAllStats(): Promise<Map<string, DriverStats>> {
    this.logger.debug('[InMemoryDriverStatsRepository] Getting all stats');
    return new Map(this.stats);
  }

  async clear(): Promise<void> {
    this.logger.info('[InMemoryDriverStatsRepository] Clearing all stats');
    this.stats.clear();
  }
}