<?php

declare(strict_types=1);

namespace Builder\Core\Providers;

use Builder\Core\Extensions\Dashboard\Widgets\SystemInfoWidget;
use Builder\Core\Extensions\Resources\Languages\LanguageResource;
use Builder\Core\Extensions\Resources\Roles\RoleResource;
use Builder\Core\Extensions\Resources\Users\UserResource;
use Builder\Core\Extensions\Settings\Pages\GeneralSettings;
use Builder\Core\Foundation\Definition\MenuItem;
use Builder\Core\Foundation\Registry;
use Illuminate\Support\ServiceProvider;

final class ExtensionServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        $registry = $this->app->make(Registry::class);

        if (config('builder.resources.users', true)) {
            $registry->resource(UserResource::class);
        }

        if (config('builder.resources.roles', true)) {
            $registry->resource(RoleResource::class);
        }

        if (config('builder.resources.languages', true)) {
            $registry->resource(LanguageResource::class);
        }

        $registry->settingsPage(GeneralSettings::class);

        $registry->menuItem(
            MenuItem::make('dashboard')
                ->label(fn (): string => (string) __('builder::builder.nav.dashboard'))
                ->icon('LayoutDashboard')
                ->namedRoute('builder.dashboard')
                ->priority(0),
        );

        $registry->menuItem(
            MenuItem::make('settings')
                ->label(fn (): string => (string) __('builder::builder.nav.settings'))
                ->icon('Settings')
                ->namedRoute('builder.settings.show', 'general')
                ->priority(90)
                ->children([
                    MenuItem::make('users')
                        ->label(fn (): string => (string) __('builder::builder.nav.users'))
                        ->namedRoute('builder.resource.index', 'users')
                        ->permission('read:users'),
                    MenuItem::make('roles')
                        ->label(fn (): string => (string) __('builder::builder.nav.roles'))
                        ->namedRoute('builder.resource.index', 'roles')
                        ->permission('read:roles'),
                    MenuItem::make('languages')
                        ->label(fn (): string => (string) __('builder::builder.nav.languages'))
                        ->namedRoute('builder.resource.index', 'languages')
                        ->permission('read:languages'),
                ]),
        );

        $registry->menuItem(
            MenuItem::make('advanced')
                ->label(fn (): string => (string) __('builder::builder.nav.advanced'))
                ->icon('Wrench')
                ->namedRoute('builder.health')
                ->priority(95)
                ->children([
                    MenuItem::make('health')
                        ->label(fn (): string => (string) __('builder::builder.nav.health'))
                        ->namedRoute('builder.health')
                        ->permission('read:health'),
                ]),
        );

        $registry->permissions('read:health');

        $registry->widget(new SystemInfoWidget($registry));
    }
}
