<?php
/**
 * @author Colinet Julien
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Provider\Payment;

use CpCreation\VitiCore\Behavior\HasPayment;
use CpCreation\VitiCore\Contact\Model\Contact;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;

final class SogeCommerceProvider implements PaymentProviderInterface
{
    public const string TYPE = 'sogecommerce';

    private string $path = '';

    /**
     * @param array<int,array{name:string,path:string}> $paths
     */
    public function __construct(
        private readonly RouterInterface $router,
        array                            $paths,
    ) {
        foreach ($paths as $path) {
            if (($path['name'] ?? null) === $this->getMethod()) {
                $this->path = (string) ($path['path'] ?? '');
                break;
            }
        }
    }

    public function checkout(Contact $contact, HasPayment $payment): RedirectResponse
    {
        return new RedirectResponse(
            $this->router->generate($this->path, [
                'id' => $payment->getId()->toString(),
            ])
        );
    }

    public function getMethod(): string
    {
        return self::TYPE;
    }
}
