<?php

namespace Modules\User\Models\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Modules\Country\Models\Resources\CountryResource;
use Modules\Type\Models\Resources\TypeResource;

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {

        $user_data = [
            $this->email,
            $this->name,
            $this->last_name,
            $this->phone,
            $this->birth_date,
            $this->gender,
            $this->country_id,
        ];
        $countFilled = count(array_filter($user_data, function($value) {
            return $value != null;
        })) ;
        $percentage = ceil( ( $countFilled / count($user_data) ) * 100);

        return [
            'id' => $this->id,
            'name'=>$this->name,
            'last_name'=>$this->last_name,
            'email'=>$this->email,
            'phone'=>$this->phone,
            'photo' => asset($this->photo),
            'role'=>'user',
            'language'=>$this->language,
            'percentage'=>$percentage,
            'gender'=>$this->gender,
            'birth_date'=>$this->birth_date,
            'type'=>new TypeResource($this->whenLoaded('type')),
            'address'=>new AddressResource($this->whenLoaded('address')),
            'country'=>new CountryResource($this->whenLoaded('country'))

        ];
    }
}
