<?php

namespace App\Http\Resources;

use App\City;
use App\Country;
use App\Helpers\ImageHelper;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @property mixed name
 * @property mixed email
 * @property mixed code
 * @property mixed phone
 * @property mixed birth_date
 * @property mixed gender
 * @property mixed city_id
 * @property mixed photo
 * @property mixed updated_at
 * @property mixed created_at
 * @property mixed id
 * @property mixed api_token
 * @property mixed escorts
 * @property mixed nationality
 * @property mixed identity
 * @property mixed country_id
 * @property mixed device_token
 * @property mixed agreement
 * @property mixed status
 */
class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name ,
            'email' => $this->email ,
            'country_id' => $this->country_id, // Country code +966
            'phone' => $this->phone,
            'nationality' => $this->nationality ? CountryResource::make(Country::withDescription($this->nationality)) : null, // Country id from country model
            'identity' => $this->identity,
            'evaluates' => EvaluateResource::collection($this->evaluates()->cursor()),
            'hotel_reservations' => HotelReservationResource::collection($this->hotelsReservations()->cursor()),
            'umrah_trip_reservations' => UmrahTripReservationResource::collection($this->umrahTripReservations()->cursor()),
            'favorite_hotels' => HotelResource::collection($this->hotelFavorites()->cursor()),
            'favorite_umrah_trips' => UmrahTripResource::collection($this->umrahFavorites()->cursor()),
            'flights' => FlightResource::collection($this->flights()->cursor()),
            'escorts' => $this->escorts,
            'escorts_count' => is_array($this->escorts) ? count($this->escorts) : 0,
            'birth_date' => $this->birth_date,
            'gender' => $this->gender,
            'city' => $this->city_id ? CityResource::make(City::withDescription(null, $this->city_id)) : null,
            'photo' => ImageHelper::getFullPath($this->photo ?? 'assets/web/images/user.png'),
            'api_token' => $this->api_token,
            'device_token' => is_string($this->device_token) ? json_decode($this->device_token, true)['token'] : $this->device_token['token'],
            'status' => $this->status,
            'agreement' => $this->agreement,
            'updated_at' => $this->updated_at,
            'created_at' => $this->created_at,
        ];
    }
}
