<?php

declare(strict_types=1);

namespace apexl\Io\modules\menu\attributes;

use apexl\Io\modules\user\interfaces\PermissionInterface;
use Attribute;
use ReflectionClass;

/**
 * Declarative, construction-free menu entry for an entity. Only set the fields you
 * want to override; unset (null) fields fall back to the host's derived defaults
 * (see HasMenuEntry::menuMetadata()). Read at route/menu build time via reflection,
 * so the framework no longer instantiates entities just to enumerate their menus.
 */
#[Attribute(Attribute::TARGET_CLASS)]
final class MenuEntry
{
    /**
     * @param PermissionInterface|PermissionInterface[]|null $permission
     */
    public function __construct(
        public ?string $name = null,
        public ?string $route = null,
        public ?string $icon = null,
        public ?int $weight = null,
        public ?string $pattern = null,
        public PermissionInterface|array|null $permission = null,
    ) {}

    /**
     * @param class-string $class
     */
    public static function on(string $class): ?self
    {
        $attributes = new ReflectionClass($class)->getAttributes(self::class);

        return $attributes === [] ? null : $attributes[0]->newInstance();
    }
}
