<?php

declare(strict_types=1);

namespace App\Tests\Unit\SuperAdmin\Application\Command\CreateEstablishment;

use App\Shared\Domain\Clock;
use App\SuperAdmin\Application\Command\CreateEstablishment\CreateEstablishmentCommand;
use App\SuperAdmin\Application\Command\CreateEstablishment\CreateEstablishmentHandler;
use App\SuperAdmin\Infrastructure\Persistence\InMemory\InMemoryEstablishmentRepository;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

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

    private InMemoryEstablishmentRepository $repository;
    private CreateEstablishmentHandler $handler;

    protected function setUp(): void
    {
        $this->repository = new InMemoryEstablishmentRepository();

        $clock = new class implements Clock {
            public function now(): DateTimeImmutable
            {
                return new DateTimeImmutable('2026-02-16 10:00:00');
            }
        };

        $this->handler = new CreateEstablishmentHandler(
            $this->repository,
            $clock,
        );
    }

    #[Test]
    public function createsEstablishmentAndReturnsResult(): void
    {
        $command = new CreateEstablishmentCommand(
            name: 'École Alpha',
            subdomain: 'ecole-alpha',
            adminEmail: 'admin@ecole-alpha.fr',
            superAdminId: self::SUPER_ADMIN_ID,
        );

        $result = ($this->handler)($command);

        self::assertNotEmpty($result->establishmentId);
        self::assertNotEmpty($result->tenantId);
        self::assertSame('École Alpha', $result->name);
        self::assertSame('ecole-alpha', $result->subdomain);
        self::assertStringStartsWith('classeo_tenant_', $result->databaseName);
    }

    #[Test]
    public function savesEstablishmentToRepository(): void
    {
        $command = new CreateEstablishmentCommand(
            name: 'École Beta',
            subdomain: 'ecole-beta',
            adminEmail: 'admin@ecole-beta.fr',
            superAdminId: self::SUPER_ADMIN_ID,
        );

        $result = ($this->handler)($command);

        $establishments = $this->repository->findAll();
        self::assertCount(1, $establishments);
        self::assertSame($result->establishmentId, (string) $establishments[0]->id);
    }
}
