<?php

declare(strict_types=1);

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

use ApiPlatform\Metadata\GetCollection;
use App\SuperAdmin\Application\Query\GetEstablishments\GetEstablishmentsHandler;
use App\SuperAdmin\Domain\Model\Establishment\Establishment;
use App\SuperAdmin\Domain\Model\SuperAdmin\SuperAdminId;
use App\SuperAdmin\Infrastructure\Api\Provider\EstablishmentCollectionProvider;
use App\SuperAdmin\Infrastructure\Persistence\InMemory\InMemoryEstablishmentRepository;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

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

    #[Test]
    public function provideReturnsEmptyArrayWhenNoEstablishments(): void
    {
        $repository = new InMemoryEstablishmentRepository();
        $handler = new GetEstablishmentsHandler($repository);
        $provider = new EstablishmentCollectionProvider($handler);

        $result = $provider->provide(new GetCollection());

        self::assertSame([], $result);
    }

    #[Test]
    public function provideReturnsMappedResources(): void
    {
        $repository = new InMemoryEstablishmentRepository();
        $repository->save(Establishment::creer(
            name: 'École Alpha',
            subdomain: 'ecole-alpha',
            createdBy: SuperAdminId::fromString(self::SUPER_ADMIN_ID),
            createdAt: new DateTimeImmutable('2026-02-16 10:00:00'),
        ));

        $handler = new GetEstablishmentsHandler($repository);
        $provider = new EstablishmentCollectionProvider($handler);

        $result = $provider->provide(new GetCollection());

        self::assertCount(1, $result);
        self::assertSame('École Alpha', $result[0]->name);
        self::assertSame('ecole-alpha', $result[0]->subdomain);
        self::assertSame('active', $result[0]->status);
    }
}
