<?php

declare(strict_types=1);

namespace App\Administration\Application\Command\UpdateSubject;

use App\Administration\Domain\Exception\SubjectDejaExistanteException;
use App\Administration\Domain\Exception\SubjectNotFoundException;
use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Administration\Domain\Model\Subject\Subject;
use App\Administration\Domain\Model\Subject\SubjectCode;
use App\Administration\Domain\Model\Subject\SubjectColor;
use App\Administration\Domain\Model\Subject\SubjectId;
use App\Administration\Domain\Model\Subject\SubjectName;
use App\Administration\Domain\Repository\SubjectRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;

use function assert;

use Symfony\Component\Messenger\Attribute\AsMessageHandler;

/**
 * Handler pour modifier une matière existante.
 */
#[AsMessageHandler(bus: 'command.bus')]
final readonly class UpdateSubjectHandler
{
    public function __construct(
        private SubjectRepository $subjectRepository,
        private Clock $clock,
    ) {
    }

    public function __invoke(UpdateSubjectCommand $command): Subject
    {
        $subjectId = SubjectId::fromString($command->subjectId);
        $tenantId = TenantId::fromString($command->tenantId);
        $schoolId = SchoolId::fromString($command->schoolId);
        $now = $this->clock->now();

        $subject = $this->subjectRepository->get($subjectId);

        // Vérifier que la matière appartient au tenant (défense en profondeur)
        if (!$subject->tenantId->equals($tenantId)) {
            throw SubjectNotFoundException::withId($subjectId);
        }

        // Mise à jour du nom
        if ($command->name !== null) {
            assert($command->name !== '');
            $subject->renommer(new SubjectName($command->name), $now);
        }

        // Mise à jour du code
        if ($command->code !== null) {
            assert($command->code !== '');
            $newCode = new SubjectCode($command->code);

            // Vérifier l'unicité du code (sauf si c'est le même)
            if (!$subject->code->equals($newCode)) {
                $existingSubject = $this->subjectRepository->findByCode($newCode, $tenantId, $schoolId);
                if ($existingSubject !== null && !$existingSubject->id->equals($subjectId)) {
                    throw SubjectDejaExistanteException::avecCode($newCode);
                }

                $subject->changerCode($newCode, $now);
            }
        }

        // Mise à jour de la couleur
        if ($command->clearColor) {
            $subject->changerCouleur(null, $now);
        } elseif ($command->color !== null) {
            assert($command->color !== '');
            $subject->changerCouleur(new SubjectColor($command->color), $now);
        }

        // Mise à jour de la description
        if ($command->clearDescription) {
            $subject->decrire(null, $now);
        } elseif ($command->description !== null) {
            $subject->decrire($command->description, $now);
        }

        $this->subjectRepository->save($subject);

        return $subject;
    }
}
