import { Injectable, Optional } from '@nestjs/common';
import { PaymentProviderPort, PaymentProviderType } from '../../domain/ports/payment-provider.port';
import { StripeProvider } from './stripe/stripe.provider';
import { AlipayProvider } from './alipay/alipay.provider';
import { WeChatPayProvider } from './wechat/wechat-pay.provider';
import { CryptoProvider } from './crypto/crypto.provider';

@Injectable()
export class PaymentProviderRegistry {
  private readonly providers = new Map<PaymentProviderType, PaymentProviderPort>();

  constructor(
    @Optional() stripe: StripeProvider,
    @Optional() alipay: AlipayProvider,
    @Optional() wechat: WeChatPayProvider,
    @Optional() crypto: CryptoProvider,
  ) {
    if (stripe?.isConfigured()) this.providers.set(PaymentProviderType.STRIPE, stripe);
    if (alipay?.isConfigured()) this.providers.set(PaymentProviderType.ALIPAY, alipay);
    if (wechat?.isConfigured()) this.providers.set(PaymentProviderType.WECHAT_PAY, wechat);
    if (crypto?.isConfigured()) this.providers.set(PaymentProviderType.CRYPTO, crypto);
  }

  get(type: PaymentProviderType): PaymentProviderPort | undefined {
    return this.providers.get(type);
  }

  getAvailable(): PaymentProviderType[] {
    return Array.from(this.providers.keys());
  }
}
