<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class TestSampleResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'sample_type' => new SampleTypeResource($this->whenLoaded('sample')),
            'selected_organisms' => $this->whenLoaded('organisms', function() {
                return $this->organisms->map(function ($item){
                    return array(
                        'id' => $item->id,
                        'is_default' => $item->is_default,
                        'organism_id' => $item->organism_id,
                        'test_sample_id' => $item->test_sample_id,
                        'organism' => $item->organism,
                    );
                });
            }),
            'test_values' => $this->whenLoaded('testValues', function (){
                return $this->testValues->map(function ($item){
                    return array(
                        'id' => $item->id,
                        'patient_type_id' => $item->patient_type_id,
                        'sign' => $item->sign,
                        'maximum' => $item->maximum,
                        'minimum' => $item->minimum
                    );
                });
            }),
            'test_id' => $this->test_id,
            'heading_id' => $this->heading_id,
            'usd_price' => $this->usd_price,
            'group_result' => $this->group_result,
            'category' => $this->category,
            'unit_sign' => $this->unit_sign,
            'weight' => $this->weight,
            'field_type' => $this->field_type,
            'format' => $this->format,
            'formula' => $this->formula,
            'description' => $this->description,
            'lab_id' => $this->lab_id,
            'code' => $this->code,
            'is_bold' => (int) $this->is_bold,
            'autocomplete_bind' => (int) $this->autocomplete_bind
        ];
    }
}
