<?php

declare(strict_types=1);

namespace App\Tests\Unit\SuperAdmin\Infrastructure\Api\Processor;

use ApiPlatform\Metadata\Post;
use App\Shared\Domain\Clock;
use App\SuperAdmin\Application\Command\CreateEstablishment\CreateEstablishmentHandler;
use App\SuperAdmin\Domain\Model\SuperAdmin\SuperAdminId;
use App\SuperAdmin\Infrastructure\Api\Processor\CreateEstablishmentProcessor;
use App\SuperAdmin\Infrastructure\Api\Resource\EstablishmentResource;
use App\SuperAdmin\Infrastructure\Persistence\InMemory\InMemoryEstablishmentRepository;
use App\SuperAdmin\Infrastructure\Security\SecuritySuperAdmin;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Security;

final class CreateEstablishmentProcessorTest extends TestCase
{
    private const string SUPER_ADMIN_ID = '550e8400-e29b-41d4-a716-446655440001';

    #[Test]
    public function processCreatesEstablishmentAndReturnsResource(): void
    {
        $repository = new InMemoryEstablishmentRepository();
        $clock = new class implements Clock {
            public function now(): DateTimeImmutable
            {
                return new DateTimeImmutable('2026-02-16 10:00:00');
            }
        };
        $handler = new CreateEstablishmentHandler($repository, $clock);

        $securityUser = new SecuritySuperAdmin(
            SuperAdminId::fromString(self::SUPER_ADMIN_ID),
            'superadmin@classeo.fr',
            'hashed',
        );

        $security = $this->createMock(Security::class);
        $security->method('getUser')->willReturn($securityUser);

        $processor = new CreateEstablishmentProcessor($handler, $security);

        $input = new EstablishmentResource();
        $input->name = 'École Gamma';
        $input->subdomain = 'ecole-gamma';
        $input->adminEmail = 'admin@ecole-gamma.fr';

        $result = $processor->process($input, new Post());

        self::assertNotNull($result->id);
        self::assertNotNull($result->tenantId);
        self::assertSame('École Gamma', $result->name);
        self::assertSame('ecole-gamma', $result->subdomain);
        self::assertSame('active', $result->status);
    }
}
