<?php

declare(strict_types=1);

namespace App\Administration\Application\Command\UploadLogo;

use App\Administration\Application\Service\LogoUploader;
use App\Administration\Domain\Model\SchoolBranding\SchoolBranding;
use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Administration\Domain\Repository\SchoolBrandingRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler(bus: 'command.bus')]
final readonly class UploadLogoHandler
{
    public function __construct(
        private SchoolBrandingRepository $brandingRepository,
        private LogoUploader $logoUploader,
        private Clock $clock,
    ) {
    }

    public function __invoke(UploadLogoCommand $command): SchoolBranding
    {
        $tenantId = TenantId::fromString($command->tenantId);
        $schoolId = SchoolId::fromString($command->schoolId);
        $now = $this->clock->now();

        $branding = $this->brandingRepository->findBySchoolId($schoolId, $tenantId);

        if ($branding === null) {
            $branding = SchoolBranding::creer(
                schoolId: $schoolId,
                tenantId: $tenantId,
                createdAt: $now,
            );
        }

        $logoUrl = $this->logoUploader->upload($command->file, $tenantId, $branding->logoUrl);
        $branding->changerLogo($logoUrl, $now);

        $this->brandingRepository->save($branding);

        return $branding;
    }
}
