<?php

namespace AutoAdmin\Bundle\Tests\Unit\Seo\Redirect;

use AutoAdmin\Bundle\Content\Frontend\FrontendUrlChecker;
use AutoAdmin\Bundle\Seo\Entity\Redirect;
use AutoAdmin\Bundle\Seo\Redirect\RedirectService;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class RedirectServiceTest extends TestCase
{
    /**
     * @dataProvider dataProviderTestGetRedirectResponse
     */
    public function testGetRedirectResponse(
        Request $request,
        bool $rootRedirect,
        ?Redirect $redirect,
        ?Response $expectedResponse
    )
    {
        $frontEndUrlChecker = $this->getMockBuilder(FrontendUrlChecker::class)
            ->disableOriginalConstructor()
            ->getMock();
        $frontEndUrlChecker->method('isFrontUrl')->willReturn(true);

        $redirectService = $this->getMockBuilder(RedirectService::class)
            ->disableOriginalConstructor()
            ->getMock();
        $redirectService->method('getRedirect')->willReturn($redirect);

        $refl = new ReflectionClass(RedirectService::class);
        $method = $refl->getMethod('getRedirectResponse');

        /** @var Response $response */
        $response = $method->invokeArgs($redirectService, [$request, $rootRedirect]);

        if ($response === null) {
            $this->assertEquals($expectedResponse, $response);
        } else {
            $this->assertEquals($expectedResponse->headers->get('Location'), $response->headers->get('Location'));
            $this->assertEquals($expectedResponse->getStatusCode(), $response->getStatusCode());
        }
    }

    public function dataProviderTestGetRedirectResponse(): array
    {
        return [
            'global_ok' => [
                'request' => Request::create('/myurl'),
                'rootRedirect' => true,
                'redirect' => (new Redirect())
                    ->setOrigin('/myurl')
                    ->setDestination('/redirected')
                    ->setType(Redirect::REDIRECT_302)
                    ->setRootRedirect(true)
                ,
                'response' => new Response('', Redirect::REDIRECT_302, ['Location' => '/redirected']),
            ],
            'global_ko' => [
                'request' => Request::create('/myurl'),
                'rootRedirect' => false,
                'redirect' => (new Redirect())
                    ->setOrigin('/myurl')
                    ->setDestination('/redirected')
                    ->setType(Redirect::REDIRECT_302)
                    ->setRootRedirect(true)
                ,
                'response' => new Response('', Redirect::REDIRECT_302, ['Location' => '/redirected']),
            ],
            'not_global_ok' => [
                'request' => Request::create('/myurl'),
                'rootRedirect' => false,
                'redirect' => (new Redirect())
                    ->setOrigin('/myurl')
                    ->setDestination('/redirected')
                    ->setType(Redirect::REDIRECT_302)
                    ->setRootRedirect(false)
                ,
                'response' => new Response('', Redirect::REDIRECT_302, ['Location' => '/redirected']),
            ],
            'not_global_ko' => [
                'request' => Request::create('/myurl'),
                'rootRedirect' => false,
                'redirect' => (new Redirect())
                    ->setOrigin('/myurl')
                    ->setDestination('/redirected')
                    ->setType(Redirect::REDIRECT_302)
                    ->setRootRedirect(true)
                ,
                'response' => new Response('', Redirect::REDIRECT_302, ['Location' => '/redirected']),
            ],
            'redir_code_check_301' => [
                'request' => Request::create('/myurl'),
                'rootRedirect' => true,
                'redirect' => (new Redirect())
                    ->setOrigin('/myurl')
                    ->setDestination('/redirected')
                    ->setType(Redirect::REDIRECT_301)
                    ->setRootRedirect(true)
                ,
                'response' => new Response('', Redirect::REDIRECT_301, ['Location' => '/redirected']),
            ],
            'redir_code_check_302' => [
                'request' => Request::create('/myurl'),
                'rootRedirect' => true,
                'redirect' => (new Redirect())
                    ->setOrigin('/myurl')
                    ->setDestination('/redirected')
                    ->setType(Redirect::REDIRECT_302)
                    ->setRootRedirect(true)
                ,
                'response' => new Response('', Redirect::REDIRECT_302, ['Location' => '/redirected']),
            ],
            'no_redir' => [
                'request' => Request::create('/myurl'),
                'rootRedirect' => true,
                'redirect' => null,
                'response' => null,
            ],
        ];
    }

    /**
     * @dataProvider dataProviderTestReplacePlaceholders
     */
    public function testReplacePlaceholders(string $uri, string $origin, string $destination, string $expected)
    {
        $reflect = new ReflectionClass(RedirectService::class);
        $method = $reflect->getMethod('replacePlaceholders');

        $frontEndUrlChecker = $this->getMockBuilder(FrontendUrlChecker::class)
            ->disableOriginalConstructor()
            ->getMock();
        $frontEndUrlChecker->method('isFrontUrl')->willReturn(true);

        $redirectService = $this->getMockBuilder(RedirectService::class)
            ->disableOriginalConstructor()
            ->getMock();

        $redirect = $this->getMockBuilder(Redirect::class)
            ->disableOriginalConstructor()
            ->getMock();

        $redirect->expects($this->once())
            ->method('getDestination')
            ->willReturn($destination);

        $redirect->expects($this->once())
            ->method('getOrigin')
            ->willReturn($origin);

        $this->assertEquals($expected, $method->invokeArgs($redirectService, [$redirect, $uri]));
    }

    public function dataProviderTestReplacePlaceholders(): array
    {
        return [
            // Test redirect classic
            'normal' => [
                '/old-page',
                '/old-page',
                '/new-page',
                '/new-page',
            ],
            // Test redirect with '**' at the end
            'multiple-end' => [
                '/old-page/some/path',
                '/old-page/**',
                '/new-page/**',
                '/new-page/some/path',
            ],
            // Test redirect with '**' at a middle
            'multiple-middle' => [
                '/old-page/some/path',
                '/old-**/some/path',
                '/new-**/some/path',
                '/new-page/some/path',
            ],
            // Test redirect with wildcard in origin URL
            'single-end' => [
                '/old-something/page',
                '/old-something/*',
                '/new-page',
                '/new-page',
            ],
            // Test redirect with wildcard in origin URL
            'single-middle' => [
                '/old-something/page',
                '/old-*/page',
                '/new-page',
                '/new-page',
            ],
            // Test redirect with numerated wildcard in origin URL
            'single-numerated' => [
                '/old-page/1',
                '/old-page/*1*',
                '/new-page/*1*',
                '/new-page/1',
            ],
            // Test redirect with multiple wildcard in origin URL
            'multiple-wildcard' => [
                '/old-page/1/2/3',
                '/old-page/*1*/*2*/*3*',
                '/new-page/*1*/*2*/*3*',
                '/new-page/1/2/3',
            ],
            // Test redirect with multiple mixed wildcard in origin URL
            'multiple-mixed-wildcard' => [
                '/old-page/1/2/3',
                '/old-page/*1*/*2*/*3*',
                '/new-page/*3*/*1*/*2*',
                '/new-page/3/1/2',
            ],
        ];
    }
}
