<?php

declare(strict_types=1);

namespace App\SuperAdmin\Infrastructure\Api\Provider;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\SuperAdmin\Application\Query\GetEstablishments\GetEstablishmentsHandler;
use App\SuperAdmin\Application\Query\GetEstablishments\GetEstablishmentsQuery;
use App\SuperAdmin\Infrastructure\Api\Resource\EstablishmentResource;
use Override;

/**
 * @implements ProviderInterface<EstablishmentResource>
 */
final readonly class EstablishmentCollectionProvider implements ProviderInterface
{
    public function __construct(
        private GetEstablishmentsHandler $handler,
    ) {
    }

    /**
     * @return EstablishmentResource[]
     */
    #[Override]
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
    {
        $views = ($this->handler)(new GetEstablishmentsQuery());

        return array_map(
            static function ($view): EstablishmentResource {
                $resource = new EstablishmentResource();
                $resource->id = $view->id;
                $resource->tenantId = $view->tenantId;
                $resource->name = $view->name;
                $resource->subdomain = $view->subdomain;
                $resource->status = $view->status;
                $resource->createdAt = $view->createdAt;
                $resource->lastActivityAt = $view->lastActivityAt;

                return $resource;
            },
            $views,
        );
    }
}
