<?php


namespace API\Service\Http\Resources;


use Carbon\Carbon;
use Mamad\Service\Repository\ServiceRepository;
use Illuminate\Http\Resources\Json\JsonResource;
use API\Category\Http\Resources\CategoryResource;

class ServiceResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {

        $cities = [];
        foreach ($this->regions as $region){
            $cities[$region->city->name][$region->id] =[
            "id"=>$region->id,
            "name"=>$region->name,
            ];
        }

        return [
            'id' => $this->id,
            'title' => $this->name,
            'slug' => $this->slug,
            'url' => $this->url,
            'days_provide' => $this->days_provide,
            'regions' => $this->regions,
            'cities' => $cities,

            'created_at' => IR_TimestampToDate(Carbon::parse($this->created_at)->format("d-m-Y"),'Y-n-j'),
            'updated_at' => Carbon::parse($this->updated_at)->format("d-m-Y"),

            'media' => [
                "alt"=>$this->name,
                "url"=>$this->thumb(),
            ],
            'parent' => $this->parent,
            'children' => ServiceResource::collection($this->children),
            'description' => $this->description,
            'questions' => $this->questions,
            'service_comment' => $this->service_comment,

        ];
    }
}
