import { Season } from '@core/racing/domain/entities/season/Season';
import { SeasonRepository } from '@core/racing/domain/repositories/SeasonRepository';
import { Logger } from '@core/shared/domain';

export class InMemorySeasonRepository implements SeasonRepository {
  private seasons: Map<string, Season> = new Map(); // Key: seasonId

  constructor(private readonly logger: Logger) {
    this.logger.info('InMemorySeasonRepository initialized.');
  }

  async findById(id: string): Promise<Season | null> {
    this.logger.debug(`[InMemorySeasonRepository] Finding season by ID: ${id}`);
    const season = this.seasons.get(id) ?? null;
    if (season) {
      this.logger.info(`Found season by ID: ${id}.`);
    } else {
      this.logger.warn(`Season with ID ${id} not found.`);
    }
    return Promise.resolve(season);
  }

  async findByLeagueId(leagueId: string): Promise<Season[]> {
    this.logger.debug(`[InMemorySeasonRepository] Finding seasons by league ID: ${leagueId}`);
    const seasons = Array.from(this.seasons.values()).filter(season => season.leagueId === leagueId);
    this.logger.info(`Found ${seasons.length} seasons for league ID: ${leagueId}.`);
    return Promise.resolve(seasons);
  }

  async create(season: Season): Promise<Season> {
    this.logger.debug(`[InMemorySeasonRepository] Creating season: ${season.id} (${season.name})`);
    if (this.seasons.has(season.id)) {
      this.logger.warn(`Season with ID ${season.id} already exists.`);
      throw new Error('Season already exists');
    }
    this.seasons.set(season.id, season);
    this.logger.info(`Season ${season.id} created successfully.`);
    return Promise.resolve(season);
  }

  async add(season: Season): Promise<void> {
    this.logger.debug(`[InMemorySeasonRepository] Adding season: ${season.id} (${season.name})`);
    if (this.seasons.has(season.id)) {
        this.logger.warn(`Season with ID ${season.id} already exists (add method).`);
        throw new Error('Season already exists');
    }
    this.seasons.set(season.id, season);
    this.logger.info(`Season ${season.id} added successfully.`);
    return Promise.resolve();
  }

  async update(season: Season): Promise<void> {
    this.logger.debug(`[InMemorySeasonRepository] Updating season: ${season.id} (${season.name})`);
    if (!this.seasons.has(season.id)) {
      this.logger.warn(`Season with ID ${season.id} not found for update.`);
      throw new Error('Season not found');
    }
    this.seasons.set(season.id, season);
    this.logger.info(`Season ${season.id} updated successfully.`);
    return Promise.resolve();
  }

  async delete(id: string): Promise<void> {
    this.logger.debug(`[InMemorySeasonRepository] Deleting season with ID: ${id}`);
    if (this.seasons.delete(id)) {
      this.logger.info(`Season ${id} deleted successfully.`);
    } else {
      this.logger.warn(`Season with ID ${id} not found for deletion.`);
    }
    return Promise.resolve();
  }

  async listByLeague(leagueId: string): Promise<Season[]> {
    this.logger.debug(`[InMemorySeasonRepository] Listing seasons by league ID: ${leagueId}`);
    const seasons = Array.from(this.seasons.values()).filter(season => season.leagueId === leagueId);
    return Promise.resolve(seasons);
  }

  async listActiveByLeague(leagueId: string): Promise<Season[]> {
    this.logger.debug(`[InMemorySeasonRepository] Listing active seasons by league ID: ${leagueId}`);
    const activeSeasons = Array.from(this.seasons.values()).filter(
      season => season.leagueId === leagueId && season.status.isActive(),
    );
    return Promise.resolve(activeSeasons);
  }

  clear(): void {
    this.seasons.clear();
  }
}
