<?php

declare(strict_types=1);

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

use App\Administration\Application\Query\GetAssignment\GetAssignmentHandler;
use App\Administration\Application\Query\GetAssignment\GetAssignmentQuery;
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 GetAssignmentHandlerTest 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 SUBJECT_ID = '550e8400-e29b-41d4-a716-446655440030';
    private const string ACADEMIC_YEAR_ID = '550e8400-e29b-41d4-a716-446655440040';

    private InMemoryTeacherAssignmentRepository $repository;
    private GetAssignmentHandler $handler;

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

    #[Test]
    public function returnsNullWhenNoAssignmentFound(): void
    {
        $result = ($this->handler)(new GetAssignmentQuery(
            teacherId: self::TEACHER_ID,
            classId: self::CLASS_ID,
            subjectId: self::SUBJECT_ID,
            academicYearId: self::ACADEMIC_YEAR_ID,
            tenantId: self::TENANT_ID,
        ));

        self::assertNull($result);
    }

    #[Test]
    public function returnsAssignmentDto(): void
    {
        $assignment = $this->createAndSaveAssignment();

        $result = ($this->handler)(new GetAssignmentQuery(
            teacherId: self::TEACHER_ID,
            classId: self::CLASS_ID,
            subjectId: self::SUBJECT_ID,
            academicYearId: self::ACADEMIC_YEAR_ID,
            tenantId: self::TENANT_ID,
        ));

        self::assertInstanceOf(TeacherAssignmentDto::class, $result);
        self::assertSame((string) $assignment->id, $result->id);
        self::assertSame(self::TEACHER_ID, $result->teacherId);
        self::assertSame(self::CLASS_ID, $result->classId);
        self::assertSame(self::SUBJECT_ID, $result->subjectId);
    }

    #[Test]
    public function returnsNullForRemovedAssignment(): void
    {
        $assignment = $this->createAndSaveAssignment();
        $assignment->retirer(new DateTimeImmutable('2026-02-11 10:00:00'));
        $this->repository->save($assignment);

        $result = ($this->handler)(new GetAssignmentQuery(
            teacherId: self::TEACHER_ID,
            classId: self::CLASS_ID,
            subjectId: self::SUBJECT_ID,
            academicYearId: self::ACADEMIC_YEAR_ID,
            tenantId: self::TENANT_ID,
        ));

        self::assertNull($result);
    }

    #[Test]
    public function dtoContainsCorrectData(): void
    {
        $createdAt = new DateTimeImmutable('2026-02-10 10:00:00');
        $this->createAndSaveAssignment();

        $result = ($this->handler)(new GetAssignmentQuery(
            teacherId: self::TEACHER_ID,
            classId: self::CLASS_ID,
            subjectId: self::SUBJECT_ID,
            academicYearId: self::ACADEMIC_YEAR_ID,
            tenantId: self::TENANT_ID,
        ));

        self::assertSame(self::ACADEMIC_YEAR_ID, $result->academicYearId);
        self::assertSame('active', $result->status);
        self::assertEquals($createdAt, $result->startDate);
        self::assertNull($result->endDate);
        self::assertEquals($createdAt, $result->createdAt);
    }

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

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

        return $assignment;
    }
}
