<?php

namespace Modules\Linksehat\Transformers\Speciality;

use App\Helpers\Helper;
use Illuminate\Http\Resources\Json\JsonResource;

class SpecialityResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        if ($this->practices && $this->prices) {
            foreach ($this->practices as $practice) {
                if ($practice->service_code === 'walkin') {
                    $practices[] = $practice->id;
                }
            }

            foreach ($this->prices as $price) {
                if (in_array($price->priceable_id, $practices)) {
                    $prices = $price->price_net;
                }
            }
        }

        return [
            $this->mergeWhen($this->practitioner && $this->prices, [
                'id' => isset($this->practitioner->person->id) ? $this->practitioner->person->id : $this->practitioner_id,
                'name' => isset($this->practitioner->person->name) ? $this->practitioner->person->name : $this->person_name,
                'specialis' => 'Spesialis ' . (isset($this->speciality->name) ? $this->speciality->name : $this->speciality_name),
                'experience' => rand(5, 12),
                'rating' => rand(20, 100),
                'price' => isset($prices) ? Helper::currencyIdrFormat($prices) : null,
            ]),
            $this->mergeWhen($this->name, [
                'title' => 'Spesialis ' . $this->name,
                'value' => $this->name
            ])
        ];
    }
}
