<?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\EventListener;

use Ferienpass\CoreBundle\EventListener\PaymentRoutesListener;
use Ferienpass\CoreBundle\Payments\PaymentSystem;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;

final class PaymentRoutesListenerTest extends TestCase
{
    public function testThrowsNotFoundOnPaymentsRouteWhenPaymentsDisabled(): void
    {
        $this->expectException(NotFoundHttpException::class);

        $listener = new PaymentRoutesListener($this->paymentSystem(enabled: false));
        $listener($this->event(['_requires_payments' => true]));
    }

    public function testAllowsPaymentsRouteWhenPaymentsEnabledWithoutOnlineProvider(): void
    {
        // Cash-only tenant: payments enabled, no online provider.
        $listener = new PaymentRoutesListener($this->paymentSystem(enabled: true, onlineProvider: false));
        $listener($this->event(['_requires_payments' => true]));

        $this->addToAssertionCount(1);
    }

    public function testThrowsNotFoundOnProviderRouteWhenNoOnlineProvider(): void
    {
        $this->expectException(NotFoundHttpException::class);

        // Cash-only tenant must not expose the online "pay now" routes.
        $listener = new PaymentRoutesListener($this->paymentSystem(enabled: true, onlineProvider: false));
        $listener($this->event(['_requires_payment_provider' => true]));
    }

    public function testAllowsProviderRouteWhenOnlineProviderConfigured(): void
    {
        $listener = new PaymentRoutesListener($this->paymentSystem(enabled: true, onlineProvider: true));
        $listener($this->event(['_requires_payment_provider' => true]));

        $this->addToAssertionCount(1);
    }

    public function testIgnoresRoutesWithoutPaymentFlag(): void
    {
        $listener = new PaymentRoutesListener($this->paymentSystem(enabled: false, onlineProvider: false));
        $listener($this->event([]));

        $this->addToAssertionCount(1);
    }

    public function testIgnoresSubRequests(): void
    {
        $listener = new PaymentRoutesListener($this->paymentSystem(enabled: false, onlineProvider: false));
        $listener($this->event(['_requires_payments' => true], HttpKernelInterface::SUB_REQUEST));

        $this->addToAssertionCount(1);
    }

    private function paymentSystem(bool $enabled, bool $onlineProvider = false): PaymentSystem
    {
        $paymentSystem = $this->createStub(PaymentSystem::class);
        $paymentSystem->method('isEnabled')->willReturn($enabled);
        $paymentSystem->method('hasOnlineProvider')->willReturn($onlineProvider);

        return $paymentSystem;
    }

    private function event(array $attributes, int $requestType = HttpKernelInterface::MAIN_REQUEST): RequestEvent
    {
        $request = new Request(attributes: $attributes);

        return new RequestEvent($this->createStub(HttpKernelInterface::class), $request, $requestType);
    }
}
