<?php

namespace Modules\Linksehat\Transformers;

use App\Helpers\Helper;
use Illuminate\Http\Resources\Json\JsonResource;
use Modules\Linksehat\Transformers\Speciality\SpecialityResource;

class DoctorResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        // $practices = $this->practitionerRoles->map(function($practitionerRole) {
        //     return [
        //         'id' => $practitionerRole->id,
        //         'doctor_id' => $practitionerRole->practitioner_id,
        //         'hospital_id' => $practitionerRole->organization_id,
        //         'speciality_id' => $practitionerRole->speciality_id,
        //         'hospital' => HospitalResource::make($practitionerRole->organization),
        //         'speciality' => SpecialityResource::make($practitionerRole->speciality),
        //         'str' => '3121100888826697',
        //         'sip' => '73/B.16/31.71.05.1003.01.015.S.2/4/-1.779.3/e/2020',

        //         'is_chat_available' => $practitionerRole->is_chat_available,
        //         'is_video_available' => $practitionerRole->is_video_available,
        //         'is_walkin_available' => $practitionerRole->is_walkin_available,
        //         'is_instant_chat_available' => $practitionerRole->is_instant_chat_available,
        //     ];
        // });

        $specialities = [];
        $hospitals = [];
        $isChatAvailable = false;
        $isVideoAvailable = false;
        $isWalkinAvailable = false;
        $isInstantChatAvailable = false;

        // Get Price Range
        foreach ($this->practitionerRoles as $practitionerRole) {
            foreach ($practitionerRole->practices as $practice) {
                foreach ($practice->prices as $price) {
                    $price_nets[] = $price->price_net;
                }
            }
        }

        foreach ($this->practitionerRoles as $practitionerRole) {
            $specialities[$practitionerRole->speciality_id]['id'] = $practitionerRole->speciality_id;
            $specialities[$practitionerRole->speciality_id]['code'] = $practitionerRole->speciality->code;
            $specialities[$practitionerRole->speciality_id]['name'] = $practitionerRole->speciality->name;
            // $specialities[$practitionerRole->speciality_id]['practices'][] = [
            //     'id' => $practitionerRole->id,
            //     'doctor_id' => $practitionerRole->practitioner_id,
            //     'hospital_id' => $practitionerRole->organization_id,
            //     'speciality_id' => $practitionerRole->speciality_id,
            //     'hospital' => HospitalResource::make($practitionerRole->organization),
            //     'str' => '3121100888826697',
            //     'sip' => '73/B.16/31.71.05.1003.01.015.S.2/4/-1.779.3/e/2020',

            //     'is_chat_available' => $practitionerRole->is_chat_available,
            //     'is_video_available' => $practitionerRole->is_video_available,
            //     'is_walkin_available' => $practitionerRole->is_walkin_available,
            //     'is_instant_chat_available' => $practitionerRole->is_instant_chat_available,
            // ];


            $hospitals[$practitionerRole->organization_id]['id'] = $practitionerRole->hospital_id;
            // $hospitals[$practitionerRole->organization_id]['code'] = $practitionerRole->organization->code;
            $hospitals[$practitionerRole->organization_id]['name'] = $practitionerRole->organization->name;
            $hospitals[$practitionerRole->organization_id]['description'] = $practitionerRole->organization->description;
            $hospitals[$practitionerRole->organization_id]['address'] = $practitionerRole->organization->currentAddress->text;
            $hospitals[$practitionerRole->organization_id]['lat'] = $practitionerRole->organization->currentAddress->lat;
            $hospitals[$practitionerRole->organization_id]['lng'] = $practitionerRole->organization->currentAddress->lng;
            $hospitals[$practitionerRole->organization_id]['distance'] = $practitionerRole->organization->currentAddress->distance ?? null;
            $hospitals[$practitionerRole->organization_id]['city_name'] = $practitionerRole->organization->currentAddress->city->name ?? null;
            $hospitals[$practitionerRole->organization_id]['rating'] = (rand(350, 500) / 100);
            $hospitals[$practitionerRole->organization_id]['phone'] = $practitionerRole->organization->meta->phone;
            $hospitals[$practitionerRole->organization_id]['photo_url'] = asset('images/default-hospital-image.png');

            $hospitals[$practitionerRole->organization_id]['practices'][] = [
                'id' => $practitionerRole->id,
                'doctor_id' => $practitionerRole->practitioner_id,
                'hospital_id' => $practitionerRole->organization_id,
                'speciality_id' => $practitionerRole->speciality_id,
                'speciality' => SpecialityResource::make($practitionerRole->speciality),
                'str' => '3121100888826697',
                'sip' => '73/B.16/31.71.05.1003.01.015.S.2/4/-1.779.3/e/2020',

                'is_chat_available' => $practitionerRole->is_chat_available,
                'is_video_available' => $practitionerRole->is_video_available,
                'is_walkin_available' => $practitionerRole->is_walkin_available,
                'is_instant_chat_available' => $practitionerRole->is_instant_chat_available,
            ];

            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;
            }
        }

        return [
            'id' => $this->id,
            'full_name' => $this->person->full_name ?? null,
            'name_prefix' => $this->person->name_prefix ?? null,
            'name' => $this->person->name ?? null,
            'name_suffix' => $this->person->name_suffix ?? null,
            'gender' => $this->person->gender ?? null,
            'is_online' => false,
            'is_insurance_covered' => rand(0, 1) == 1,
            'price_range' => Helper::currencyIdrFormat(min($price_nets)) . ' - ' . Helper::currencyIdrFormat(max($price_nets)),
            'price_start' => Helper::currencyIdrFormat(min($price_nets)), // min
            'price_end' => Helper::currencyIdrFormat(max($price_nets)), // max
            'currency' => 'IDR',
            'avatar_url' => asset('images/default-doctor-avatar.png'),
            'education' => $this->meta->education ?? '',
            'medical_treatment' => $this->meta->medical_treatment ?? '',
            'award' => $this->meta->award ?? '',
            'work_experience' => $this->meta->work_experience ?? '',
            'keilmuan' => $this->meta->keilmuan ?? '',
            'length_of_work_year' => rand(1, 10),
            'description' => $this->meta->description ?? '',
            // 'practices' => $practices,
            'rating' => rand(88, 100),
            'specialities' => $specialities,
            'hospitals' => $hospitals,

            'is_has_chat_available' => $isChatAvailable,
            'is_has_video_available' => $isVideoAvailable,
            'is_has_walkin_available' => $isWalkinAvailable,
            'is_has_instant_chat_available' => $isInstantChatAvailable,
        ];
    }
}
