<?php

namespace Modules\Payment\Factories;

use Modules\Payment\Contracts\PaymentGatewayInterface;
use Modules\Payment\Enums\PaymentMethod;
use RuntimeException;

/**
 * Resolves a PaymentGatewayInterface implementation by PaymentMethod.
 *
 * Replaces bnf_event's 3x duplicated `switch($payment_type)` at each call
 * site (domain.md §6). Call sites depend only on this factory, never on a
 * concrete gateway class — swapping/adding a gateway is a `register()` call
 * here, no controller/action changes.
 */
class PaymentGatewayFactory
{
    /**
     * @var array<string, class-string<PaymentGatewayInterface>>
     */
    private array $bindings = [];

    /**
     * @param  class-string<PaymentGatewayInterface>  $gatewayClass
     */
    public function register(PaymentMethod $method, string $gatewayClass): void
    {
        $this->bindings[$method->value] = $gatewayClass;
    }

    public function make(PaymentMethod $method): PaymentGatewayInterface
    {
        $gatewayClass = $this->bindings[$method->value]
            ?? throw new RuntimeException("No payment gateway registered for method [{$method->value}].");

        return app($gatewayClass);
    }
}
