import { Module } from "@nestjs/common";
import { HttpModule } from "@nestjs/axios";
import {
  CardProvider,
  CacBankProvider,
  CbeBirrProvider,
  DMoneyProvider,
  EBirrProvider,
  PaymentProvider,
  TelebirrProvider,
  WaafiProvider,
} from "@edr/payment-providers";
import { ProviderMethod } from "@edr/types";

/** Injection token for the Map<ProviderMethod, PaymentProvider> used to select a gateway. */
export const PAYMENT_PROVIDER_MAP = Symbol("PAYMENT_PROVIDER_MAP");

export type PaymentProviderMap = Map<ProviderMethod, PaymentProvider>;

const providerClasses = [
  TelebirrProvider,
  CbeBirrProvider,
  EBirrProvider,
  CardProvider,
  WaafiProvider,
  DMoneyProvider,
  CacBankProvider,
];

/**
 * Thin DI wiring around @edr/payment-providers — the exact provider set the passenger app
 * used to construct, relocated here. After cutover this service is the only consumer of the
 * provider SDK and of the provider secrets (config/{waafi,telebirr,…}.config.ts).
 */
@Module({
  imports: [HttpModule.register({ timeout: 10_000 })],
  providers: [
    ...providerClasses,
    {
      provide: PAYMENT_PROVIDER_MAP,
      useFactory: (...providers: PaymentProvider[]): PaymentProviderMap =>
        new Map(providers.map((provider) => [provider.method, provider])),
      inject: providerClasses,
    },
  ],
  exports: [PAYMENT_PROVIDER_MAP, ...providerClasses],
})
export class ProvidersModule {}
