<?php

declare(strict_types=1);

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

use App\Administration\Application\Query\GetAssignmentsForTeacher\GetAssignmentsForTeacherHandler;
use App\Administration\Application\Query\GetAssignmentsForTeacher\GetAssignmentsForTeacherQuery;
use App\Administration\Application\Query\GetAssignmentsForTeacher\TeacherAssignmentDto;
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 GetAssignmentsForTeacherHandlerTest extends TestCase
{
    private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
    private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
    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 GetAssignmentsForTeacherHandler $handler;

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

    #[Test]
    public function returnsEmptyWhenNoAssignments(): void
    {
        $result = ($this->handler)(new GetAssignmentsForTeacherQuery(
            teacherId: self::TEACHER_ID,
            tenantId: self::TENANT_ID,
        ));

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

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

        $result = ($this->handler)(new GetAssignmentsForTeacherQuery(
            teacherId: self::TEACHER_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-446655440030');

        $removed = TeacherAssignment::creer(
            tenantId: TenantId::fromString(self::TENANT_ID),
            teacherId: UserId::fromString(self::TEACHER_ID),
            classId: ClassId::fromString(self::CLASS_ID),
            subjectId: SubjectId::fromString('550e8400-e29b-41d4-a716-446655440031'),
            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 GetAssignmentsForTeacherQuery(
            teacherId: self::TEACHER_ID,
            tenantId: self::TENANT_ID,
        ));

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

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

        $result = ($this->handler)(new GetAssignmentsForTeacherQuery(
            teacherId: self::TEACHER_ID,
            tenantId: '550e8400-e29b-41d4-a716-446655440099',
        ));

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

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

        $result = ($this->handler)(new GetAssignmentsForTeacherQuery(
            teacherId: self::TEACHER_ID,
            tenantId: self::TENANT_ID,
        ));

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

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

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