<?php

namespace App\Http\Resources\Planning;

use App\Helpers\ImageSize;
use App\Http\Resources\Developer;
use App\Planning;
use App\Section;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Collection;

class PlanningCollection extends ResourceCollection
{
    /**
     * @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 : [];
    }

    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $result = [];
        /** @var Planning $planning */
        foreach ($this->collection as $key => $planning) {
            /** @var Section $section */
            $section = $planning->section;
            $developer = $section->residental_complex && $section->residental_complex->developer ?
                $section->residental_complex->developer : null;

            $result[$section->id]['id'] = $section->id;
            $result[$section->id]['building_name'] = $section->name;
            $result[$section->id]['status'] = $section->stage_of_construction ? $section->stage_of_construction->code : "";
            $result[$section->id]['year'] = $section->date_finish_buildiing;
            $result[$section->id]['quarterly'] = $section->quarterly_finish ? $section->quarterly_finish->code : '';
            $result[$section->id]['developer'] = $developer ? new Developer($developer) : [];
            $result[$section->id]['planning'][] = [
                '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') : ""
                ]
            ];
            $result[$section->id]['count'] = count($result[$section->id]['planning']);
        }

        $collection = collect($result);
        $sortSection = $collection->sortBy('building_name');

        return $sortSection;
    }
}
