/**
 * In-Memory Implementation: InMemoryPrizeRepository
 */

import type { Logger } from '@core/shared/application/Logger';
import type { IPrizeRepository } from '@core/payments/domain/repositories/IPrizeRepository';
import type { Prize } from '@core/payments/domain/entities/Prize';

const prizes: Map<string, Prize> = new Map();

export class InMemoryPrizeRepository implements IPrizeRepository {
  constructor(private readonly logger: Logger) {}

  async findById(id: string): Promise<Prize | null> {
    this.logger.debug('[InMemoryPrizeRepository] findById', { id });
    return prizes.get(id) || null;
  }

  async findByLeagueId(leagueId: string): Promise<Prize[]> {
    this.logger.debug('[InMemoryPrizeRepository] findByLeagueId', { leagueId });
    return Array.from(prizes.values()).filter(p => p.leagueId === leagueId);
  }

  async findByLeagueIdAndSeasonId(leagueId: string, seasonId: string): Promise<Prize[]> {
    this.logger.debug('[InMemoryPrizeRepository] findByLeagueIdAndSeasonId', { leagueId, seasonId });
    return Array.from(prizes.values()).filter(
      p => p.leagueId === leagueId && p.seasonId === seasonId
    );
  }

  async findByPosition(leagueId: string, seasonId: string, position: number): Promise<Prize | null> {
    this.logger.debug('[InMemoryPrizeRepository] findByPosition', { leagueId, seasonId, position });
    return Array.from(prizes.values()).find(
      p => p.leagueId === leagueId && p.seasonId === seasonId && p.position === position
    ) || null;
  }

  async create(prize: Prize): Promise<Prize> {
    this.logger.debug('[InMemoryPrizeRepository] create', { prize });
    prizes.set(prize.id, prize);
    return prize;
  }

  async update(prize: Prize): Promise<Prize> {
    this.logger.debug('[InMemoryPrizeRepository] update', { prize });
    prizes.set(prize.id, prize);
    return prize;
  }

  async delete(id: string): Promise<void> {
    this.logger.debug('[InMemoryPrizeRepository] delete', { id });
    prizes.delete(id);
  }
}