<?php

namespace App\Http\Resources\Planning;

use App\Helpers\ImageSize;
use Illuminate\Http\Resources\Json\JsonResource;
use App\ResidentialComplex;

class Planning extends JsonResource
{
    /**
     * @var array
     */
    private $parameters;

    /**
     * Create a new resource instance.
     *
     * @param  mixed $resource
     * @param array $parameters
     */
    public function __construct($resource, $parameters = null)
    {
        parent::__construct($resource);
        $this->resource = $resource;
        $this->parameters = is_array($parameters) ? $parameters : [];
    }

    public function toArray($request)
    {
        /** @var \App\Planning $planning */
        $planning = $this;

        $residentialComplex = isset($this->parameters['residential_complex']) &&
            $this->parameters['residential_complex'] instanceof ResidentialComplex ?
            $this->parameters['residential_complex'] : null;

        return [
            'id' => $planning->id,
            'room_count' => $planning->room ? $planning->room->count : "",
            'room_type' => $planning->room_type ? $planning->room_type->name : "",
            'floors' => $planning->floor_id->map(function ($item) {
                return $item->name;
            }),
            'space' => $planning->space ? $planning->space : 0,
            'cost' => $planning->total_cost ? $planning->total_cost : 0,
            'image' => [
                'default' => $planning->image ? asset(env('UPLOAD_PATH') .
                    '/' . ImageSize::PLANNING . '/' . $planning->image) : "",
                'webp' => $planning->image ? asset(env('UPLOAD_PATH') .
                    '/' . ImageSize::PLANNING_WEBP . '/' . $planning->image . '.webp') : ""
            ],
            'image_full' => [
                'default' => $planning->image ? asset(env('UPLOAD_PATH') .
                    '/' . ImageSize::PLANNING_FULL . '/' . $planning->image) : "",
                'webp' => $planning->image ? asset(env('UPLOAD_PATH') .
                    '/' . ImageSize::PLANNING_FULL_WEBP . '/' . $planning->image . '.webp') : ""
            ],
            'residential_complex' => [
                'id' => $residentialComplex ? $residentialComplex->id : "",
                'name' => $residentialComplex ? $residentialComplex->name : "",
                'slug' =>  $residentialComplex ? $residentialComplex->code : "",
            ]
        ];
    }
}
