<?php

declare(strict_types=1);

namespace App\Tests\Unit\Administration\Application\Query\CheckImageRights;

use App\Administration\Application\Query\CheckImageRights\CheckImageRightsHandler;
use App\Administration\Application\Query\CheckImageRights\CheckImageRightsQuery;
use App\Administration\Domain\Exception\UserNotFoundException;
use App\Administration\Domain\Model\User\Email;
use App\Administration\Domain\Model\User\ImageRightsStatus;
use App\Administration\Domain\Model\User\Role;
use App\Administration\Domain\Model\User\User;
use App\Administration\Domain\Model\User\UserId;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryUserRepository;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

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

    private InMemoryUserRepository $userRepository;
    private CheckImageRightsHandler $handler;

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

    #[Test]
    public function authorizedStudentCanPublish(): void
    {
        $student = $this->createStudentWithStatus(ImageRightsStatus::AUTHORIZED);

        $query = new CheckImageRightsQuery(
            studentId: (string) $student->id,
            tenantId: self::TENANT_ID,
        );

        $result = ($this->handler)($query);

        self::assertTrue($result->canPublish);
        self::assertNull($result->warningMessage);
        self::assertSame(ImageRightsStatus::AUTHORIZED, $result->status);
    }

    #[Test]
    public function refusedStudentCannotPublish(): void
    {
        $student = $this->createStudentWithStatus(ImageRightsStatus::REFUSED);

        $query = new CheckImageRightsQuery(
            studentId: (string) $student->id,
            tenantId: self::TENANT_ID,
        );

        $result = ($this->handler)($query);

        self::assertFalse($result->canPublish);
        self::assertNotNull($result->warningMessage);
        self::assertStringContainsString('PAS l\'autorisation', $result->warningMessage);
    }

    #[Test]
    public function notSpecifiedStudentCannotPublish(): void
    {
        $student = $this->createStudentWithStatus(ImageRightsStatus::NOT_SPECIFIED);

        $query = new CheckImageRightsQuery(
            studentId: (string) $student->id,
            tenantId: self::TENANT_ID,
        );

        $result = ($this->handler)($query);

        self::assertFalse($result->canPublish);
        self::assertNotNull($result->warningMessage);
        self::assertStringContainsString('pas renseigné', $result->warningMessage);
    }

    #[Test]
    public function throwsWhenStudentNotFound(): void
    {
        $this->expectException(UserNotFoundException::class);

        $query = new CheckImageRightsQuery(
            studentId: '550e8400-e29b-41d4-a716-446655440000',
            tenantId: self::TENANT_ID,
        );

        ($this->handler)($query);
    }

    #[Test]
    public function throwsWhenTenantMismatch(): void
    {
        $student = $this->createStudentWithStatus(ImageRightsStatus::AUTHORIZED);

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

        $query = new CheckImageRightsQuery(
            studentId: (string) $student->id,
            tenantId: '550e8400-e29b-41d4-a716-446655440099',
        );

        ($this->handler)($query);
    }

    private function createStudentWithStatus(ImageRightsStatus $status): User
    {
        $student = User::creer(
            email: new Email('eleve@example.com'),
            role: Role::ELEVE,
            tenantId: TenantId::fromString(self::TENANT_ID),
            schoolName: 'École Alpha',
            dateNaissance: new DateTimeImmutable('2012-06-15'),
            createdAt: new DateTimeImmutable('2026-01-15 10:00:00'),
        );

        if ($status !== ImageRightsStatus::NOT_SPECIFIED) {
            $student->modifierDroitImage(
                $status,
                UserId::fromString('550e8400-e29b-41d4-a716-446655440099'),
                new DateTimeImmutable(),
            );
        }

        $this->userRepository->save($student);

        return $student;
    }
}
