<?php

declare(strict_types=1);

namespace App\SuperAdmin\Infrastructure\Api\Provider;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\SuperAdmin\Application\Query\GetEstablishmentsMetrics\GetEstablishmentsMetricsHandler;
use App\SuperAdmin\Application\Query\GetEstablishmentsMetrics\GetEstablishmentsMetricsQuery;
use App\SuperAdmin\Infrastructure\Api\Resource\MetricsResource;
use Override;

/**
 * @implements ProviderInterface<MetricsResource>
 */
final readonly class MetricsProvider implements ProviderInterface
{
    public function __construct(
        private GetEstablishmentsMetricsHandler $handler,
    ) {
    }

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

        return array_map(
            static function ($view): MetricsResource {
                $resource = new MetricsResource();
                $resource->establishmentId = $view->establishmentId;
                $resource->name = $view->name;
                $resource->status = $view->status;
                $resource->userCount = $view->userCount;
                $resource->studentCount = $view->studentCount;
                $resource->teacherCount = $view->teacherCount;
                $resource->lastLoginAt = $view->lastLoginAt;

                return $resource;
            },
            $metrics,
        );
    }
}
