<?php

namespace Modules\Linksehat\Transformers\Doctor;

use App\Helpers\Helper;
use App\Models\Organization;
use Illuminate\Http\Resources\Json\JsonResource;
use Modules\Linksehat\Transformers\Hospital\HospitalResource;

class DoctorResourceDetail extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        foreach ($this->practitionerRoles as $practitionerRole) {
            foreach ($practitionerRole->practices as $practice) {
                foreach ($practice->prices as $price) {
                    $prices[$practice->service_code] = !empty($price->price_net) ? Helper::currencyIdrFormat($price->price_net) : '-';
                }
            }

            // Spesialis
            $spesialis[$practitionerRole->speciality_id]['id'] = $practitionerRole->speciality->id;
            $spesialis[$practitionerRole->speciality_id]['name'] = $practitionerRole->speciality->name;
            $spesialis[$practitionerRole->speciality_id]['str'] = random_int(1000000000000000, 5000000000000000);

            if ($practitionerRole->is_chat_available) {
                $isChatAvailable = true;
            }
            if ($practitionerRole->is_video_available) {
                $isVideoAvailable = true;
            }
            if ($practitionerRole->is_walkin_available) {
                $isWalkinAvailable = true;
            }
            if ($practitionerRole->is_instant_chat_available) {
                $isInstantChatAvailable = true;
            }
        }

        $award[] = explode("\n", $this->meta->award);
        $education[] = explode("\n", $this->meta->education);

        $queryHospitals = Organization::query()
            ->without('meta')
            ->where('organizations.type', 'hospital')
            ->leftJoin('addresses', function ($q) {
                $q->on('organizations.main_address_id', '=', 'addresses.id');
                $q->where('addresses.addressable_type', '=', Organization::class);
            });
        if ($request->has('lat') && !empty($request->lat) && $request->has('lng') && !empty($request->lng)) {
            $queryHospitals = $queryHospitals->selectRaw("organizations.*, addresses.text AS currentAddress, 6371 * acos (cos ( radians($request->lat) ) * cos( radians( addresses.lat ) ) * cos( radians( addresses.lng ) - radians($request->lng) ) + sin ( radians($request->lat) ) * sin( radians( addresses.lat ) )) as distance")->orderBy('distance', 'ASC');
        }
        $queryHospitals = $queryHospitals->orderBy('organizations.name', 'asc')->where('organizations.id', $this->organization_id)->get();

        return [
            'name' => $this->person->full_name ?? null,
            'length_of_work_year' => rand(1, 20),
            'price' => empty($prices) ? null : $prices,
            'rating' => rand(50, 100),
            'photos' => [
                'title' => 'doctor-avatar-' . $this->person->id ?? null,
                'url' => asset('images/default-doctor-avatar.png')
            ],
            'specialities' => empty($spesialis) ? null : $spesialis,
            'award' => empty($award) ? null : $award,
            'education' => empty($education) ? null : $education,
            'location_practect' => empty($queryHospitals) ? null : HospitalResource::collection($queryHospitals),
            'practice_at' => empty($queryHospitals) ? null : HospitalResource::collection($queryHospitals),
            'available' => [
                'is_chat_available' => $isChatAvailable ?? null,
                'is_video_available' => $isVideoAvailable ?? null,
                'is_walkin_available' => $isWalkinAvailable ?? null,
                'is_instant_chat_available' => $isInstantChatAvailable ?? null,
                'day_available' => $this->day_available ?? null
            ]
        ];
    }
}
