<?php

namespace App\DTOs;

use App\Enums\MenuLocation;
use App\Enums\MenuType;

final readonly class MenuData
{
    public function __construct(
        public string $label,
        public string $url,
        public MenuLocation $location,
        public MenuType $type,
        public ?int $parentId,
        public int $orderIndex = 0,
        public bool $isActive = true,
    ) {}

    /**
     * @param  array<string, mixed>  $data
     */
    public static function fromArray(array $data): self
    {
        return new self(
            label: $data['label'],
            url: $data['url'],
            location: MenuLocation::from($data['location']),
            type: MenuType::from($data['type']),
            parentId: $data['parent_id'] ?? null,
            orderIndex: $data['order_index'] ?? 0,
            isActive: $data['is_active'] ?? true,
        );
    }

    /**
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'label' => $this->label,
            'url' => $this->url,
            'location' => $this->location->value,
            'type' => $this->type->value,
            'parent_id' => $this->parentId,
            'order_index' => $this->orderIndex,
            'is_active' => $this->isActive,
        ];
    }
}
