<?php

declare(strict_types=1);

namespace Ferienpass\AdminBundle\Tests\Controller\Page;

use Ferienpass\AdminBundle\Controller\Page\LostPasswordController;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Attribute\Route;

final class LostPasswordControllerTest extends TestCase
{
    public function testClassHasPasswordResetRoutePrefix(): void
    {
        $ref = new \ReflectionClass(LostPasswordController::class);
        $attrs = $ref->getAttributes(Route::class);

        self::assertCount(1, $attrs);
        self::assertSame('/passwort-vergessen', $attrs[0]->newInstance()->getPath());
    }

    public function testIndexRouteIsNamedAdminLostPassword(): void
    {
        $ref = new \ReflectionMethod(LostPasswordController::class, 'index');
        $attrs = $ref->getAttributes(Route::class);

        self::assertCount(1, $attrs);
        $route = $attrs[0]->newInstance();
        self::assertSame('', $route->getPath());
        self::assertSame('admin_lost_password', $route->getName());
    }

    public function testCheckEmailRouteIsNamedCorrectly(): void
    {
        $ref = new \ReflectionMethod(LostPasswordController::class, 'checkEmail');
        $attrs = $ref->getAttributes(Route::class);

        self::assertCount(1, $attrs);
        $route = $attrs[0]->newInstance();
        self::assertSame('admin_lost_password_check_email', $route->getName());
    }

    public function testResetRouteAcceptsTokenParameter(): void
    {
        $ref = new \ReflectionMethod(LostPasswordController::class, 'reset');
        $attrs = $ref->getAttributes(Route::class);

        self::assertCount(1, $attrs);
        $route = $attrs[0]->newInstance();
        self::assertSame('admin_lost_password_reset', $route->getName());
        self::assertStringContainsString('{token}', $route->getPath());
    }

    public function testControllerUsesResetPasswordControllerTrait(): void
    {
        $ref = new \ReflectionClass(LostPasswordController::class);
        $traits = $ref->getTraitNames();

        self::assertContains(
            'SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait',
            $traits,
        );
    }

    public function testAllRoutesAreNamed(): void
    {
        $ref = new \ReflectionClass(LostPasswordController::class);
        $routes = [];

        foreach ($ref->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
            if ($method->getDeclaringClass()->getName() !== LostPasswordController::class) {
                continue;
            }

            foreach ($method->getAttributes(Route::class) as $attr) {
                $route = $attr->newInstance();
                if (null !== $route->getName()) {
                    $routes[] = $route->getName();
                }
            }
        }

        self::assertContains('admin_lost_password', $routes);
        self::assertContains('admin_lost_password_check_email', $routes);
        self::assertContains('admin_lost_password_reset', $routes);
    }
}
