<?php

declare(strict_types=1);

namespace App\Tests\Unit\SuperAdmin\Application\Query\GetEstablishments;

use App\SuperAdmin\Application\Query\GetEstablishments\GetEstablishmentsHandler;
use App\SuperAdmin\Application\Query\GetEstablishments\GetEstablishmentsQuery;
use App\SuperAdmin\Domain\Model\Establishment\Establishment;
use App\SuperAdmin\Domain\Model\SuperAdmin\SuperAdminId;
use App\SuperAdmin\Infrastructure\Persistence\InMemory\InMemoryEstablishmentRepository;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

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

    private InMemoryEstablishmentRepository $repository;
    private GetEstablishmentsHandler $handler;

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

    #[Test]
    public function returnsEmptyArrayWhenNoEstablishments(): void
    {
        $result = ($this->handler)(new GetEstablishmentsQuery());

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

    #[Test]
    public function returnsAllEstablishments(): void
    {
        $this->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'),
        ));

        $this->repository->save(Establishment::creer(
            name: 'École Beta',
            subdomain: 'ecole-beta',
            createdBy: SuperAdminId::fromString(self::SUPER_ADMIN_ID),
            createdAt: new DateTimeImmutable('2026-02-16 11:00:00'),
        ));

        $result = ($this->handler)(new GetEstablishmentsQuery());

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