<?php

declare(strict_types=1);

namespace App\Application\Admin\ValueObjects;

final readonly class NavigationItem
{
    public function __construct(
        public string $name,
        public string $url,
        public ?string $icon = null,
        public bool $isActive = false
    ) {
    }

    public static function fromArray(array $data): self
    {
        return new self(
            name: $data['name'] ?? '',
            url: $data['url'] ?? '',
            icon: $data['icon'] ?? null,
            isActive: $data['is_active'] ?? false
        );
    }

    public function toArray(): array
    {
        return [
            'name' => $this->name,
            'url' => $this->url,
            'icon' => $this->icon,
            'is_active' => $this->isActive,
        ];
    }

    public function withActiveState(bool $isActive): self
    {
        return new self(
            name: $this->name,
            url: $this->url,
            icon: $this->icon,
            isActive: $isActive
        );
    }

    public function isActive(string $currentPath): bool
    {
        return $this->url === $currentPath || str_starts_with($currentPath, $this->url);
    }
}
