import { Protest } from '@core/racing/domain/entities/Protest';
import { ProtestRepository } from '@core/racing/domain/repositories/ProtestRepository';
import { Logger } from '@core/shared/domain';

export class InMemoryProtestRepository implements ProtestRepository {
  private protests: Map<string, Protest> = new Map();

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

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

  async findByRaceId(raceId: string): Promise<Protest[]> {
    this.logger.debug(`[InMemoryProtestRepository] Finding protests for race: ${raceId}.`);
    const protests = Array.from(this.protests.values()).filter(p => p.raceId === raceId);
    this.logger.info(`Found ${protests.length} protests for race ${raceId}.`);
    return Promise.resolve(protests);
  }

  async findByProtestingDriverId(driverId: string): Promise<Protest[]> {
    this.logger.debug(`[InMemoryProtestRepository] Finding protests by protesting driver ID: ${driverId}.`);
    const protests = Array.from(this.protests.values()).filter(p => p.protestingDriverId === driverId);
    this.logger.info(`Found ${protests.length} protests by protesting driver ${driverId}.`);
    return Promise.resolve(protests);
  }

  async findByAccusedDriverId(driverId: string): Promise<Protest[]> {
    this.logger.debug(`[InMemoryProtestRepository] Finding protests by accused driver ID: ${driverId}.`);
    const protests = Array.from(this.protests.values()).filter(p => p.accusedDriverId === driverId);
    this.logger.info(`Found ${protests.length} protests by accused driver ${driverId}.`);
    return Promise.resolve(protests);
  }

  async findPending(): Promise<Protest[]> {
    this.logger.debug('[InMemoryProtestRepository] Finding all pending protests.');
    const pendingProtests = Array.from(this.protests.values()).filter(p => p.status.toString() === 'pending');
    this.logger.info(`Found ${pendingProtests.length} pending protests.`);
    return Promise.resolve(pendingProtests);
  }

  async findUnderReviewBy(stewardId: string): Promise<Protest[]> {
    this.logger.debug(`[InMemoryProtestRepository] Finding protests under review by steward: ${stewardId}.`);
    const underReviewProtests = Array.from(this.protests.values()).filter(p => p.reviewedBy === stewardId && p.status.toString() === 'under_review');
    this.logger.info(`Found ${underReviewProtests.length} protests under review by steward ${stewardId}.`);
    return Promise.resolve(underReviewProtests);
  }

  async create(protest: Protest): Promise<void> {
    this.logger.debug(`[InMemoryProtestRepository] Creating protest: ${protest.id}.`);
    if (this.protests.has(protest.id)) {
      this.logger.warn(`Protest with ID ${protest.id} already exists.`);
      throw new Error('Protest already exists');
    }
    this.protests.set(protest.id, protest);
    this.logger.info(`Protest ${protest.id} created successfully.`);
    return Promise.resolve();
  }

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

  async exists(id: string): Promise<boolean> {
    this.logger.debug(`[InMemoryProtestRepository] Checking existence of protest with ID: ${id}.`);
    return Promise.resolve(this.protests.has(id));
  }
}
