using GoodBooks.PAY.PAYBLL.Abstractions;
using GoodBooks.PAY.PAYBLL.Providers;
using Microsoft.Extensions.Logging;
namespace GoodBooks.PAY.PAYBLL.Factory
{
///
/// Resolves IPaymentGateway by gateway code using a switch expression.
/// Follows the ComplianceAdapterFactory pattern — no keyed DI.
/// Registered as Singleton (stateless) in Program.cs.
///
/// HttpClient instances are created fresh per call via IHttpClientFactory to
/// ensure thread-safety and correct header isolation between concurrent requests.
///
public sealed class PaymentGatewayFactory : IPaymentGatewayFactory
{
private readonly IHttpClientFactory _httpFactory;
private readonly ILoggerFactory _loggerFactory;
public PaymentGatewayFactory(IHttpClientFactory httpFactory, ILoggerFactory loggerFactory)
{
_httpFactory = httpFactory;
_loggerFactory = loggerFactory;
}
public IPaymentGateway GetGateway(string gatewayCode) =>
gatewayCode.ToUpperInvariant() switch
{
"RAZORPAY" => new RazorpayGateway(
_httpFactory.CreateClient("Razorpay"),
_loggerFactory.CreateLogger()),
"STRIPE" => new StripeGateway(
_httpFactory.CreateClient("Stripe"),
_loggerFactory.CreateLogger()),
"CASHFREE" => new CashfreeGateway(
_httpFactory.CreateClient("Cashfree"),
_loggerFactory.CreateLogger()),
_ => throw new NotSupportedException(
$"Payment gateway '{gatewayCode}' is not supported. " +
$"Supported gateways: RAZORPAY, STRIPE, CASHFREE.")
};
}
}