<?php

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Manager;

use CpCreation\VitiCore\Behavior\HasPayment;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Provider\Payment\PaymentProviderInterface;
use CpCreation\VitiCore\Contact\Model\Contact;

final class PaymentProviderManager
{
    /** @var list<PaymentProviderInterface> */
    private array $providers = [];

    public function addProvider(PaymentProviderInterface $provider): void
    {
        $this->providers[] = $provider;
    }

    /**
     * @throws \RuntimeException
     */
    public function createPaymentProviderManager(Contact $contact, HasPayment $payment): mixed
    {
        $method = $payment->getPaymentMethod()?->getMethod();

        foreach ($this->providers as $provider) {
            if ($provider->getMethod() === $method) {
                return $provider->checkout($contact, $payment);
            }
        }

        throw new \RuntimeException('Pas de provider de paiement trouvé');
    }
}
