/**
 * In-Memory Implementation: InMemoryPaymentRepository
 */

import type { Logger } from '@core/shared/application/Logger';
import type { IPaymentRepository } from '@core/payments/domain/repositories/IPaymentRepository';
import type { Payment, PaymentType } from '@core/payments/domain/entities/Payment';

const payments: Map<string, Payment> = new Map();

export class InMemoryPaymentRepository implements IPaymentRepository {
  constructor(private readonly logger: Logger) {}

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

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

  async findByPayerId(payerId: string): Promise<Payment[]> {
    this.logger.debug('[InMemoryPaymentRepository] findByPayerId', { payerId });
    return Array.from(payments.values()).filter(p => p.payerId === payerId);
  }

  async findByType(type: PaymentType): Promise<Payment[]> {
    this.logger.debug('[InMemoryPaymentRepository] findByType', { type });
    return Array.from(payments.values()).filter(p => p.type === type);
  }

  async findByFilters(filters: { leagueId?: string; payerId?: string; type?: PaymentType }): Promise<Payment[]> {
    this.logger.debug('[InMemoryPaymentRepository] findByFilters', { filters });
    let results = Array.from(payments.values());

    if (filters.leagueId) {
      results = results.filter(p => p.leagueId === filters.leagueId);
    }
    if (filters.payerId) {
      results = results.filter(p => p.payerId === filters.payerId);
    }
    if (filters.type) {
      results = results.filter(p => p.type === filters.type);
    }

    return results;
  }

  async create(payment: Payment): Promise<Payment> {
    this.logger.debug('[InMemoryPaymentRepository] create', { payment });
    payments.set(payment.id, payment);
    return payment;
  }

  async update(payment: Payment): Promise<Payment> {
    this.logger.debug('[InMemoryPaymentRepository] update', { payment });
    payments.set(payment.id, payment);
    return payment;
  }
}