<?php

declare(strict_types=1);

namespace App\Scolarite\Infrastructure\Api\Resource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\Scolarite\Domain\Model\Evaluation\ClassStatistics;
use App\Scolarite\Infrastructure\Api\Provider\EvaluationStatisticsProvider;

#[ApiResource(
    shortName: 'EvaluationStatistics',
    operations: [
        new Get(
            uriTemplate: '/evaluations/{evaluationId}/statistics',
            provider: EvaluationStatisticsProvider::class,
            name: 'get_evaluation_statistics',
        ),
    ],
)]
final class EvaluationStatisticsResource
{
    #[ApiProperty(identifier: true)]
    public ?string $evaluationId = null;

    public ?float $average = null;

    public ?float $min = null;

    public ?float $max = null;

    public ?float $median = null;

    public int $gradedCount = 0;

    public static function fromStatistics(string $evaluationId, ClassStatistics $statistics): self
    {
        $resource = new self();
        $resource->evaluationId = $evaluationId;
        $resource->average = $statistics->average;
        $resource->min = $statistics->min;
        $resource->max = $statistics->max;
        $resource->median = $statistics->median;
        $resource->gradedCount = $statistics->gradedCount;

        return $resource;
    }
}
