<?php

namespace Modules\Linksehat\Transformers\Hospital;

use Illuminate\Http\Resources\Json\JsonResource;
use Modules\Linksehat\Transformers\Speciality\SpecialityForHospitalDetailResource;

class HospitalResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        if ($this->specialities) {
            $specialities = SpecialityForHospitalDetailResource::collection($this->specialities);
        } else {
            $specialities = [];
        }

        return [
            'id' => $this->id,
            'name' => $this->name,
            'code' => $this->code,
            'description' => $this->description,
            'address' => $this->address ?? null,
            'lat' => $this->lat,
            'lng' => $this->lng,
            'distance' => $this->distance ? ($this->distance < 1 ? round($this->distance * 1000, 2) . ' m' : round($this->distance, 2) . ' km') : null,
            'city_name' => $this->city_name ?? null,
            'rating' => rand(1, 100),
            'phone' => $this->meta->phone,
            'photo_url' => url('images/default-hospital-image.png'),
            'photos' => [
                'title' => $this->name,
                'photo_url' => url('images/default-hospital-image.png'),
            ],
            $this->mergeWhen($this->specialities, [
                'specialities' => $specialities
            ])
        ];
    }
}
