<?php

declare(strict_types=1);

namespace App\Tests\Unit\Administration\Application\Command;

use App\Administration\Application\Command\UpdateUserRoles\UpdateUserRolesCommand;
use App\Administration\Application\Command\UpdateUserRoles\UpdateUserRolesHandler;
use App\Administration\Application\Port\ActiveRoleStore;
use App\Administration\Domain\Model\User\Email;
use App\Administration\Domain\Model\User\Role;
use App\Administration\Domain\Model\User\User;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryUserRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

final class UpdateUserRolesHandlerTest extends TestCase
{
    private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';

    private InMemoryUserRepository $userRepository;
    private UpdateUserRolesHandler $handler;
    private ActiveRoleStore $activeRoleStore;

    protected function setUp(): void
    {
        $this->userRepository = new InMemoryUserRepository();

        $clock = new class implements Clock {
            public function now(): DateTimeImmutable
            {
                return new DateTimeImmutable('2026-02-08 10:00:00');
            }
        };

        $this->activeRoleStore = $this->createMock(ActiveRoleStore::class);

        $this->handler = new UpdateUserRolesHandler($this->userRepository, $clock, $this->activeRoleStore);
    }

    #[Test]
    public function itUpdatesRolesBulk(): void
    {
        $user = $this->createUser(Role::PROF);
        $this->userRepository->save($user);

        $result = ($this->handler)(new UpdateUserRolesCommand(
            userId: (string) $user->id,
            roles: [Role::PARENT->value, Role::VIE_SCOLAIRE->value],
        ));

        self::assertFalse($result->aLeRole(Role::PROF));
        self::assertTrue($result->aLeRole(Role::PARENT));
        self::assertTrue($result->aLeRole(Role::VIE_SCOLAIRE));
    }

    #[Test]
    public function itKeepsExistingRolesIfInTargetList(): void
    {
        $user = $this->createUser(Role::PROF);
        $user->attribuerRole(Role::PARENT, new DateTimeImmutable());
        $this->userRepository->save($user);

        $result = ($this->handler)(new UpdateUserRolesCommand(
            userId: (string) $user->id,
            roles: [Role::PROF->value, Role::PARENT->value, Role::ADMIN->value],
        ));

        self::assertTrue($result->aLeRole(Role::PROF));
        self::assertTrue($result->aLeRole(Role::PARENT));
        self::assertTrue($result->aLeRole(Role::ADMIN));
        self::assertCount(3, $result->roles);
    }

    #[Test]
    public function itThrowsWhenEmptyRoles(): void
    {
        $user = $this->createUser(Role::PROF);
        $this->userRepository->save($user);

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Au moins un rôle est requis.');

        ($this->handler)(new UpdateUserRolesCommand(
            userId: (string) $user->id,
            roles: [],
        ));
    }

    #[Test]
    public function itThrowsWhenInvalidRole(): void
    {
        $user = $this->createUser(Role::PROF);
        $this->userRepository->save($user);

        $this->expectException(InvalidArgumentException::class);

        ($this->handler)(new UpdateUserRolesCommand(
            userId: (string) $user->id,
            roles: ['ROLE_INVALID'],
        ));
    }

    private function createUser(Role $role): User
    {
        return User::creer(
            email: new Email('user@example.com'),
            role: $role,
            tenantId: TenantId::fromString(self::TENANT_ID),
            schoolName: 'École Alpha',
            dateNaissance: null,
            createdAt: new DateTimeImmutable('2026-01-15 10:00:00'),
        );
    }
}
