<?php

declare(strict_types=1);

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

use App\Administration\Application\Query\GetAssignmentsForTeacher\TeacherAssignmentDto;
use App\Administration\Application\Query\GetTeachersForClass\GetTeachersForClassHandler;
use App\Administration\Application\Query\GetTeachersForClass\GetTeachersForClassQuery;
use App\Administration\Domain\Model\SchoolClass\AcademicYearId;
use App\Administration\Domain\Model\SchoolClass\ClassId;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\TeacherAssignment\TeacherAssignment;
use App\Administration\Domain\Model\User\UserId;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryTeacherAssignmentRepository;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

final class GetTeachersForClassHandlerTest extends TestCase
{
    private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
    private const string CLASS_ID = '550e8400-e29b-41d4-a716-446655440020';
    private const string ACADEMIC_YEAR_ID = '550e8400-e29b-41d4-a716-446655440040';

    private InMemoryTeacherAssignmentRepository $repository;
    private GetTeachersForClassHandler $handler;

    protected function setUp(): void
    {
        $this->repository = new InMemoryTeacherAssignmentRepository();
        $this->handler = new GetTeachersForClassHandler($this->repository);
    }

    #[Test]
    public function returnsEmptyWhenNoTeachersAssigned(): void
    {
        $result = ($this->handler)(new GetTeachersForClassQuery(
            classId: self::CLASS_ID,
            tenantId: self::TENANT_ID,
        ));

        self::assertSame([], $result);
    }

    #[Test]
    public function returnsTeachersForClass(): void
    {
        $this->createAndSaveAssignment('550e8400-e29b-41d4-a716-446655440010');
        $this->createAndSaveAssignment('550e8400-e29b-41d4-a716-446655440011');

        $result = ($this->handler)(new GetTeachersForClassQuery(
            classId: self::CLASS_ID,
            tenantId: self::TENANT_ID,
        ));

        self::assertCount(2, $result);
        self::assertContainsOnlyInstancesOf(TeacherAssignmentDto::class, $result);
    }

    #[Test]
    public function excludesRemovedAssignments(): void
    {
        $this->createAndSaveAssignment('550e8400-e29b-41d4-a716-446655440010');

        $removed = TeacherAssignment::creer(
            tenantId: TenantId::fromString(self::TENANT_ID),
            teacherId: UserId::fromString('550e8400-e29b-41d4-a716-446655440011'),
            classId: ClassId::fromString(self::CLASS_ID),
            subjectId: SubjectId::fromString('550e8400-e29b-41d4-a716-446655440030'),
            academicYearId: AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
            createdAt: new DateTimeImmutable('2026-02-10 10:00:00'),
        );
        $removed->retirer(new DateTimeImmutable('2026-02-11 10:00:00'));
        $this->repository->save($removed);

        $result = ($this->handler)(new GetTeachersForClassQuery(
            classId: self::CLASS_ID,
            tenantId: self::TENANT_ID,
        ));

        self::assertCount(1, $result);
    }

    #[Test]
    public function excludesAssignmentsFromDifferentTenant(): void
    {
        $this->createAndSaveAssignment('550e8400-e29b-41d4-a716-446655440010');

        $result = ($this->handler)(new GetTeachersForClassQuery(
            classId: self::CLASS_ID,
            tenantId: '550e8400-e29b-41d4-a716-446655440099',
        ));

        self::assertSame([], $result);
    }

    #[Test]
    public function dtoContainsCorrectData(): void
    {
        $teacherId = '550e8400-e29b-41d4-a716-446655440010';
        $createdAt = new DateTimeImmutable('2026-02-10 10:00:00');
        $this->createAndSaveAssignment($teacherId);

        $result = ($this->handler)(new GetTeachersForClassQuery(
            classId: self::CLASS_ID,
            tenantId: self::TENANT_ID,
        ));

        self::assertSame($teacherId, $result[0]->teacherId);
        self::assertSame(self::CLASS_ID, $result[0]->classId);
        self::assertSame('active', $result[0]->status);
        self::assertEquals($createdAt, $result[0]->startDate);
        self::assertNull($result[0]->endDate);
    }

    private function createAndSaveAssignment(string $teacherId): void
    {
        $assignment = TeacherAssignment::creer(
            tenantId: TenantId::fromString(self::TENANT_ID),
            teacherId: UserId::fromString($teacherId),
            classId: ClassId::fromString(self::CLASS_ID),
            subjectId: SubjectId::fromString('550e8400-e29b-41d4-a716-446655440030'),
            academicYearId: AcademicYearId::fromString(self::ACADEMIC_YEAR_ID),
            createdAt: new DateTimeImmutable('2026-02-10 10:00:00'),
        );

        $this->repository->save($assignment);
    }
}
