<?php declare(strict_types=1);

namespace App\Engine\Navigation\Menu;

class NavigationItem implements \JsonSerializable
{

    /** @var array<int, static> */
    protected array $children = [];

    public function __construct(
        protected readonly int     $id,
        protected readonly string  $title,
        protected readonly ?string $url,
        protected readonly ?int    $parentId = null,
        protected readonly bool    $targetBlank = false,
        protected readonly int     $priority = 0,
        protected bool             $isActive = false,
    ) {
    }

    public function getParentId(): ?int
    {
        return $this->parentId;
    }

    public function addChild(NavigationItem $child, int $id): static
    {
        $this->children[$id] = $child;
        return $this;
    }

    public function jsonSerialize(): array
    {
        return [
            "id" => $this->getId(),
            "title" => $this->getTitle(),
            "url" => $this->getUrl(),
            "targetBlank" => $this->isTargetBlank(),
            "priority" => $this->getPriority(),
            "isActive" => $this->getIsActive(),
            "children" => array_map(fn(NavigationItem $item) => $item->jsonSerialize(), $this->getChildren()),
        ];
    }

    public function getId(): int
    {
        return $this->id;
    }

    public function getTitle(): string
    {
        return $this->title;
    }

    public function getUrl(): ?string
    {
        return $this->url;
    }

    public function isTargetBlank(): bool
    {
        return $this->targetBlank;
    }

    public function getPriority(): int
    {
        return $this->priority;
    }

    public function getIsActive(): bool
    {
        return $this->isActive;
    }

    /**
     * @return array<int, static>
     */
    public function getChildren(): array
    {
        return $this->children;
    }

    /**
     * @param array<int, static> $children
     */
    public function setChildren(array $children): static
    {
        $this->children = $children;
        return $this;
    }

    public function setIsActive(bool $isActive): static
    {
        $this->isActive = $isActive;
        return $this;
    }

}