<?php

namespace App\Http\Controllers\Api\V1\Category\Resources;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class CategoryResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array|Arrayable|\JsonSerializable
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'parent_id' => $this->parent_id,
            'name' => $this->name,
            'slug' => $this->slug,
            'group' => $this->type,
            'description' => $this->description,
            'color' => $this->color,
            'media' => CategoryMediaResource::collection($this->whenLoaded('media')),
            'children' => static::collection($this->whenLoaded('children')),
        ];
    }
}
