import { Injectable, BadRequestException } from '@nestjs/common';
import { type PaymentProvider } from '@prisma/client';
import { BankTransferService } from './bank-transfer.service';
import { MomoService } from './momo.service';
import {
  type IPaymentGateway,
  type IPaymentGatewayFactory,
} from './payment-gateway.interface';
import { VnpayService } from './vnpay.service';
import { ZalopayService } from './zalopay.service';

@Injectable()
export class PaymentGatewayFactory implements IPaymentGatewayFactory {
  private readonly gateways: Map<PaymentProvider, IPaymentGateway>;

  constructor(
    private readonly vnpay: VnpayService,
    private readonly momo: MomoService,
    private readonly zalopay: ZalopayService,
    private readonly bankTransfer: BankTransferService,
  ) {
    this.gateways = new Map<PaymentProvider, IPaymentGateway>([
      ['VNPAY', vnpay],
      ['MOMO', momo],
      ['ZALOPAY', zalopay],
      ['BANK_TRANSFER', bankTransfer],
    ]);
  }

  getGateway(provider: PaymentProvider): IPaymentGateway {
    const gateway = this.gateways.get(provider);
    if (!gateway) {
      throw new BadRequestException(`Nhà cung cấp thanh toán không được hỗ trợ: ${provider}`);
    }
    return gateway;
  }
}
