<?php

declare(strict_types=1);

use Builder\Core\Extensions\Settings\Pages\GeneralSettings;
use Builder\Core\Foundation\Definition\MenuItem;
use Builder\Core\Foundation\Navigation\NavigationBuilder;
use Builder\Core\Foundation\Registry;
use Builder\Core\Foundation\Settings\BuilderSettings;

it('injectSettingsPageItems returns early when no settings item is in navigation', function (): void {
    $registry = new Registry;

    // Register a settings page but no 'settings' menu item
    $registry->settingsPage(GeneralSettings::class);
    $registry->menuItem(MenuItem::make('dashboard')->namedRoute('builder.dashboard')->priority(0));

    $builder = new NavigationBuilder($registry);
    $nav = $builder->build(['*']);

    // Only 'dashboard' appears; settings page items are NOT injected because
    // there is no 'settings' menu item to inject them into.
    expect($nav)->toHaveCount(1)
        ->and($nav[0]['key'])->toBe('dashboard');
});

it('buildSettingsPageItems excludes pages whose navGroup is not settings', function (): void {
    $registry = new Registry;

    $externalPage = new class extends BuilderSettings
    {
        public static function group(): string
        {
            return 'external';
        }

        public function getLabel(): string
        {
            return 'External';
        }

        public function navGroup(): string
        {
            return '';
        }
    };

    $registry->settingsPage($externalPage::class);
    $registry->menuItem(
        MenuItem::make('settings')
            ->namedRoute('builder.settings.show', 'general')
            ->children([]),
    );

    $builder = new NavigationBuilder($registry);
    $nav = $builder->build(['*']);

    $settingsChildren = $nav[0]['children'];

    expect($settingsChildren)->toBeEmpty();
});

it('buildSettingsPageItems includes pages whose navGroup is settings', function (): void {
    $registry = new Registry;

    $registry->settingsPage(GeneralSettings::class);
    $registry->menuItem(
        MenuItem::make('settings')
            ->namedRoute('builder.settings.show', 'general')
            ->children([]),
    );

    $builder = new NavigationBuilder($registry);
    $nav = $builder->build(['*']);

    $settingsChildren = $nav[0]['children'];

    expect($settingsChildren)->toHaveCount(1)
        ->and($settingsChildren[0]['key'])->toBe('general');
});
