<?php

declare(strict_types=1);

namespace App\SuperAdmin\Application\Query\GetEstablishments;

use App\SuperAdmin\Domain\Repository\EstablishmentRepository;

final readonly class GetEstablishmentsHandler
{
    public function __construct(
        private EstablishmentRepository $establishmentRepository,
    ) {
    }

    /**
     * @return EstablishmentView[]
     */
    public function __invoke(GetEstablishmentsQuery $query): array
    {
        $establishments = $this->establishmentRepository->findAll();

        return array_map(
            static fn ($e) => new EstablishmentView(
                id: (string) $e->id,
                tenantId: (string) $e->tenantId,
                name: $e->name,
                subdomain: $e->subdomain,
                status: $e->status->value,
                createdAt: $e->createdAt->format('c'),
                lastActivityAt: $e->lastActivityAt?->format('c'),
            ),
            $establishments,
        );
    }
}
