<?php

namespace App\Http\Resources\System;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class NavItemResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $item = is_array($this->resource) ? $this->resource : $this->resource->toArray();

        return [
            'id' => (int) ($item['id'] ?? 0),
            'menu_parent_id' => $item['menu_parent_id'] ?? null,
            'label' => (string) ($item['label'] ?? ''),
            'url' => $item['url'] ?? null,
            'icon_class' => $item['icon_class'] ?? null,
            'target' => $item['target'] ?? null,
            'sort_order' => (int) ($item['sort_order'] ?? 0),
            'is_enabled' => (int) ($item['is_enabled'] ?? 0),
            'children' => !empty($item['children'])
                ? NavItemResource::collection($item['children'])
                : [],
        ];
    }
}
