<?php

declare(strict_types=1);

namespace App\Tests\Unit\Administration\Infrastructure\Api\Processor;

use ApiPlatform\Metadata\Post;
use App\Administration\Application\Command\AssignTeacher\AssignTeacherHandler;
use App\Administration\Domain\Model\SchoolClass\AcademicYearId;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\SchoolClass\ClassName;
use App\Administration\Domain\Model\SchoolClass\ClassStatus;
use App\Administration\Domain\Model\SchoolClass\SchoolClass;
use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Administration\Domain\Model\SchoolClass\SchoolLevel;
use App\Administration\Domain\Model\Subject\Subject;
use App\Administration\Domain\Model\Subject\SubjectCode;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\Subject\SubjectName;
use App\Administration\Domain\Model\Subject\SubjectStatus;
use App\Administration\Domain\Model\User\Email;
use App\Administration\Domain\Model\User\Role;
use App\Administration\Domain\Model\User\StatutCompte;
use App\Administration\Domain\Model\User\User;
use App\Administration\Domain\Model\User\UserId;
use App\Administration\Infrastructure\Api\Processor\CreateTeacherAssignmentProcessor;
use App\Administration\Infrastructure\Api\Resource\TeacherAssignmentResource;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryClassRepository;
use App\Administration\Infrastructure\Persistence\InMemory\InMemorySubjectRepository;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryTeacherAssignmentRepository;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryUserRepository;
use App\Administration\Infrastructure\Security\TeacherAssignmentVoter;
use App\Administration\Infrastructure\Service\CurrentAcademicYearResolver;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use App\Shared\Infrastructure\Tenant\TenantConfig;
use App\Shared\Infrastructure\Tenant\TenantContext;
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

final class CreateTeacherAssignmentProcessorTest extends TestCase
{
    private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
    private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
    private const string CLASS_ID = '550e8400-e29b-41d4-a716-446655440020';
    private const string SUBJECT_ID = '550e8400-e29b-41d4-a716-446655440030';
    private const string ACADEMIC_YEAR_ID = '550e8400-e29b-41d4-a716-446655440040';
    private const string SCHOOL_ID = '550e8400-e29b-41d4-a716-446655440050';

    private InMemoryTeacherAssignmentRepository $repository;
    private InMemoryUserRepository $userRepository;
    private InMemoryClassRepository $classRepository;
    private InMemorySubjectRepository $subjectRepository;
    private TenantContext $tenantContext;
    private Clock $clock;

    protected function setUp(): void
    {
        $this->repository = new InMemoryTeacherAssignmentRepository();
        $this->userRepository = new InMemoryUserRepository();
        $this->classRepository = new InMemoryClassRepository();
        $this->subjectRepository = new InMemorySubjectRepository();
        $this->clock = new class implements Clock {
            public function now(): DateTimeImmutable
            {
                return new DateTimeImmutable('2026-02-10 10:00:00');
            }
        };

        $this->tenantContext = new TenantContext();
        $this->tenantContext->setCurrentTenant(new TenantConfig(
            tenantId: InfraTenantId::fromString(self::TENANT_ID),
            subdomain: 'ecole-alpha',
            databaseUrl: 'postgresql://test',
        ));

        $this->seedTestData();
    }

    private function seedTestData(): void
    {
        $tenantId = TenantId::fromString(self::TENANT_ID);

        $this->userRepository->save(User::reconstitute(
            id: UserId::fromString(self::TEACHER_ID),
            email: new Email('teacher@example.com'),
            roles: [Role::PROF],
            tenantId: $tenantId,
            schoolName: 'École Test',
            statut: StatutCompte::EN_ATTENTE,
            dateNaissance: null,
            createdAt: new DateTimeImmutable('2026-01-15'),
            hashedPassword: null,
            activatedAt: null,
            consentementParental: null,
        ));

        $this->classRepository->save(SchoolClass::reconstitute(
            id: ClassId::fromString(self::CLASS_ID),
            tenantId: $tenantId,
            schoolId: SchoolId::fromString(self::SCHOOL_ID),
            academicYearId: AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
            name: new ClassName('6ème A'),
            level: SchoolLevel::SIXIEME,
            capacity: 30,
            status: ClassStatus::ACTIVE,
            description: null,
            createdAt: new DateTimeImmutable('2026-01-15'),
            updatedAt: new DateTimeImmutable('2026-01-15'),
            deletedAt: null,
        ));

        $this->subjectRepository->save(Subject::reconstitute(
            id: SubjectId::fromString(self::SUBJECT_ID),
            tenantId: $tenantId,
            schoolId: SchoolId::fromString(self::SCHOOL_ID),
            name: new SubjectName('Mathématiques'),
            code: new SubjectCode('MATH'),
            color: null,
            status: SubjectStatus::ACTIVE,
            description: null,
            createdAt: new DateTimeImmutable('2026-01-15'),
            updatedAt: new DateTimeImmutable('2026-01-15'),
            deletedAt: null,
        ));
    }

    #[Test]
    public function createsAssignmentSuccessfully(): void
    {
        $processor = $this->createProcessor();

        $data = new TeacherAssignmentResource();
        $data->teacherId = self::TEACHER_ID;
        $data->classId = self::CLASS_ID;
        $data->subjectId = self::SUBJECT_ID;
        $data->academicYearId = self::ACADEMIC_YEAR_ID;

        $result = $processor->process($data, new Post());

        self::assertNotNull($result->id);
        self::assertSame(self::TEACHER_ID, $result->teacherId);
        self::assertSame(self::CLASS_ID, $result->classId);
        self::assertSame(self::SUBJECT_ID, $result->subjectId);
        self::assertSame('active', $result->status);
    }

    #[Test]
    public function resolvesCurrentAcademicYear(): void
    {
        $processor = $this->createProcessor();

        $data = new TeacherAssignmentResource();
        $data->teacherId = self::TEACHER_ID;
        $data->classId = self::CLASS_ID;
        $data->subjectId = self::SUBJECT_ID;
        $data->academicYearId = 'current';

        $result = $processor->process($data, new Post());

        self::assertNotNull($result->id);
        self::assertNotNull($result->academicYearId);
    }

    #[Test]
    public function throwsWhenNotAuthorized(): void
    {
        $processor = $this->createProcessor(authorized: false);

        $data = new TeacherAssignmentResource();
        $data->teacherId = self::TEACHER_ID;
        $data->classId = self::CLASS_ID;
        $data->subjectId = self::SUBJECT_ID;
        $data->academicYearId = self::ACADEMIC_YEAR_ID;

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

        $processor->process($data, new Post());
    }

    #[Test]
    public function throwsWhenTenantNotSet(): void
    {
        $emptyTenantContext = new TenantContext();
        $processor = $this->createProcessor(tenantContext: $emptyTenantContext);

        $data = new TeacherAssignmentResource();
        $data->teacherId = self::TEACHER_ID;
        $data->classId = self::CLASS_ID;
        $data->subjectId = self::SUBJECT_ID;
        $data->academicYearId = self::ACADEMIC_YEAR_ID;

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

        $processor->process($data, new Post());
    }

    #[Test]
    public function throwsConflictWhenDuplicateAssignment(): void
    {
        $processor = $this->createProcessor();

        $data = new TeacherAssignmentResource();
        $data->teacherId = self::TEACHER_ID;
        $data->classId = self::CLASS_ID;
        $data->subjectId = self::SUBJECT_ID;
        $data->academicYearId = self::ACADEMIC_YEAR_ID;

        $processor->process($data, new Post());

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

        $processor->process($data, new Post());
    }

    #[Test]
    public function throwsBadRequestForInvalidUuid(): void
    {
        $processor = $this->createProcessor();

        $data = new TeacherAssignmentResource();
        $data->teacherId = 'not-a-uuid';
        $data->classId = self::CLASS_ID;
        $data->subjectId = self::SUBJECT_ID;
        $data->academicYearId = self::ACADEMIC_YEAR_ID;

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

        $processor->process($data, new Post());
    }

    #[Test]
    public function throwsBadRequestForInvalidAcademicYearId(): void
    {
        $processor = $this->createProcessor();

        $data = new TeacherAssignmentResource();
        $data->teacherId = self::TEACHER_ID;
        $data->classId = self::CLASS_ID;
        $data->subjectId = self::SUBJECT_ID;
        $data->academicYearId = 'invalid-keyword';

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

        $processor->process($data, new Post());
    }

    private function createProcessor(
        bool $authorized = true,
        ?TenantContext $tenantContext = null,
    ): CreateTeacherAssignmentProcessor {
        $handler = new AssignTeacherHandler(
            $this->repository,
            $this->userRepository,
            $this->classRepository,
            $this->subjectRepository,
            $this->clock,
        );

        $eventBus = $this->createMock(MessageBusInterface::class);
        $eventBus->method('dispatch')->willReturnCallback(
            static fn (object $message) => new Envelope($message),
        );

        $authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
        $authorizationChecker->method('isGranted')
            ->with(TeacherAssignmentVoter::CREATE)
            ->willReturn($authorized);

        $resolvedTenantContext = $tenantContext ?? $this->tenantContext;

        $academicYearResolver = new CurrentAcademicYearResolver(
            $resolvedTenantContext,
            $this->clock,
        );

        return new CreateTeacherAssignmentProcessor(
            $handler,
            $resolvedTenantContext,
            $eventBus,
            $authorizationChecker,
            $academicYearResolver,
        );
    }
}
