<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ReportItemResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $avg = $this->resolveAverage();

        return [
            'id' => $this->id,
            'report_id' => $this->report_id,
            'product_id' => $this->product_id,
            'group_name' => $this->group_name,
            'min_value' => $this->min_value !== null ? (float) $this->min_value : null,
            'max_value' => $this->max_value !== null ? (float) $this->max_value : null,
            'value' => $this->value !== null ? (float) $this->value : null,
            'avg_value' => $avg,
            'unit' => $this->unit,
            'action_note' => $this->action_note,
            'sort_order' => $this->sort_order,
            'product' => new ProductResource($this->whenLoaded('product')),
        ];
    }
}
