<?php

namespace App\Http\Resources;

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

/**
 * @property mixed id
 * @property mixed name
 * @property mixed city_id
 * @property mixed address
 * @property mixed lat
 * @property mixed lng
 * @property mixed email
 * @property mixed images
 * @property mixed room_type
 * @property mixed room_type_price
 * @property mixed added_service
 * @property mixed added_service_price
 * @property mixed include_breakfast
 * @property mixed status
 * @property mixed created_at
 * @property mixed deleted_at
 * @property mixed first_price
 * @property mixed rate
 * @property mixed description
 * @property mixed room_id
 * @property mixed first_room_type_price
 */
class HotelResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $images = is_string($this->images) ? json_decode($this->images, true) : $this->images;
        $images = array_map(function ($value){
            return ImageHelper::getFullPath($value);
        }, $images);
        $city = $this->city_id ? (City::withDescription(null, $this->city_id)->name ?? null) : null;
//        $hotelName = descriptions(Hotel::find($this->id))->name;

        $rooms = [];
//        $addedServices = [];
//        $addedServicePrices = array_values(array_filter($this->added_service_price));
        foreach ($this->room_id as $key => $roomId) {
            $rooms[] = [
                'id' => $roomId,
                'type' => $this->room_type[$key],
                'price' => $this->room_type_price[$roomId],
                'name' => descriptions(Room::find($roomId))->name,
            ];
        }
//        foreach ($this->added_service as $serviceKey => $service) {
//            $addedServices[] = ['name' => $service, 'price' => $addedServicePrices[$serviceKey]];
//        }

        return [
            'id' => $this->id,
            'name' => descriptions($this)->name,
            'description' => descriptions($this)->description,
            'city_id' => $this->city_id,
            'city_name' => $city,
            'rate' => $this->rate,
            'evaluates_count' => $this->evaluates()->count(),
            'favorites_count' => $this->userFavorites()->count(),
            'evaluates' => EvaluateResource::collection($this->evaluates),
            'address' => $this->address,
            'lat' => $this->lat,
            'lng' => $this->lng,
            'email' => $this->email,
            'images' => $images,
            'first_room_type_price' => $this->first_room_type_price,
            'rooms' => $rooms,
            'rooms_type' => $this->room_type,
            'rooms_count' => count($rooms),
//            'added_service' => $addedServices,
            'include_breakfast' => $this->include_breakfast,
            'status' => $this->status,
            'created_at' => $this->created_at,
            'deleted_at' => $this->deleted_at,
        ];
    }
}
