<?php

namespace App\Http\Resources\ResidentialComplex;

use App\Http\Resources\ResidentialComplexImagesCollection;
use App\ResidentialComplex;
use Illuminate\Http\Resources\Json\ResourceCollection;

class ResidentialComplexSearchCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $result = [];
        /** @var ResidentialComplex $residentialComplex */
        foreach ($this->collection as $key => $residentialComplex) {
            $result[$key] = [
                'id' => $residentialComplex->id,
                'name' => $residentialComplex->name,
                'slug' => $residentialComplex->code,
                'enabled' => $residentialComplex->enabled,
                'city' => $residentialComplex->city ? $residentialComplex->city->name : "",
                'street_name' => $residentialComplex->street_name ? $residentialComplex->street_name->name : "",
                'street_type' => $residentialComplex->street_name && $residentialComplex->street_name->type ?
                    $residentialComplex->street_name->type->short_name : "",
                'street_number' => $residentialComplex->street_number,
                'images' => new ResidentialComplexImagesCollection(
                    $residentialComplex->getMedia('landscape_photo')),
            ];
        }

        return $result;
    }
}
