<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CoreBundle\Tests\Payments;

use Ferienpass\CoreBundle\Entity\Payment;
use Ferienpass\CoreBundle\Payments\Provider\NoOpPaymentProvider;
use Ferienpass\CoreBundle\Payments\Provider\PaymentProviderChain;
use Ferienpass\CoreBundle\Payments\Provider\PaymentProviderInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

final class PaymentProviderChainTest extends TestCase
{
    public function testIsConfiguredReturnsTrueWhenProviderConfigured(): void
    {
        $provider = $this->createStub(PaymentProviderInterface::class);
        $provider->method('isConfigured')->willReturn(true);

        $chain = new PaymentProviderChain([$provider]);
        self::assertTrue($chain->isConfigured());
    }

    public function testIsConfiguredReturnsFalseWhenNoneConfigured(): void
    {
        $provider = $this->createStub(PaymentProviderInterface::class);
        $provider->method('isConfigured')->willReturn(false);

        $chain = new PaymentProviderChain([$provider]);
        self::assertFalse($chain->isConfigured());
    }

    public function testIsConfiguredSkipsSelfReferences(): void
    {
        $chain = new PaymentProviderChain([]);
        $chainWithSelf = new PaymentProviderChain([$chain]);
        self::assertFalse($chainWithSelf->isConfigured());
    }

    public function testIsConfiguredReturnsFalseForEmptyProviders(): void
    {
        $chain = new PaymentProviderChain([]);
        self::assertFalse($chain->isConfigured());
    }

    public function testDispatchPaymentDelegatesToFirstConfiguredProvider(): void
    {
        $provider = $this->createMock(PaymentProviderInterface::class);
        $provider->method('isConfigured')->willReturn(true);
        $provider->expects(self::once())->method('dispatchPayment')->willReturn('https://pay.example.com');

        $chain = new PaymentProviderChain([$provider]);
        self::assertSame('https://pay.example.com', $chain->dispatchPayment($this->createStub(Payment::class)));
    }

    public function testDispatchPaymentSkipsUnconfiguredProviders(): void
    {
        $unconfigured = $this->createMock(PaymentProviderInterface::class);
        $unconfigured->method('isConfigured')->willReturn(false);
        $unconfigured->expects(self::never())->method('dispatchPayment');

        $configured = $this->createMock(PaymentProviderInterface::class);
        $configured->method('isConfigured')->willReturn(true);
        $configured->expects(self::once())->method('dispatchPayment')->willReturn('https://pay.example.com');

        $chain = new PaymentProviderChain([$unconfigured, $configured]);
        self::assertSame('https://pay.example.com', $chain->dispatchPayment($this->createStub(Payment::class)));
    }

    public function testDispatchPaymentFallsBackToNoOpProvider(): void
    {
        $this->expectException(\BadMethodCallException::class);

        $unconfigured = $this->createStub(PaymentProviderInterface::class);
        $unconfigured->method('isConfigured')->willReturn(false);

        $chain = new PaymentProviderChain([$unconfigured, new NoOpPaymentProvider()]);
        $chain->dispatchPayment($this->createStub(Payment::class));
    }

    public function testDispatchPaymentThrowsRuntimeWhenNoFallback(): void
    {
        $this->expectException(\RuntimeException::class);

        $chain = new PaymentProviderChain([]);
        $chain->dispatchPayment($this->createStub(Payment::class));
    }

    public function testInterpretRedirectDelegatesToConfiguredProvider(): void
    {
        $provider = $this->createMock(PaymentProviderInterface::class);
        $provider->method('isConfigured')->willReturn(true);
        $provider->expects(self::once())->method('interpretRedirect')->willReturn(42);

        $chain = new PaymentProviderChain([$provider]);
        self::assertSame(42, $chain->interpretRedirect(new Request()));
    }
}
