<?php

namespace App\Providers;

use App\Models\User;
use App\Nova\CategoryResource;
use App\Nova\ConfigResource;
use App\Nova\LogResource;
use App\Nova\QuestionGroupResource;
use App\Nova\QuestionResource;
use App\Nova\ScreeningResource;
use App\Nova\SessionResource;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Laravel\Fortify\Features;
use Laravel\Nova\Menu\MenuItem;
use Laravel\Nova\Menu\MenuSection;
use Laravel\Nova\Nova;
use Laravel\Nova\NovaApplicationServiceProvider;

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        parent::boot();

        Nova::mainMenu(function (Request $request) {
            return [
                MenuSection::make('Main', [
                    MenuItem::link('Dashboard', '/dashboards/main'),
                    MenuItem::externalLink('To questionnaire ↗', '/'),
                ])->icon('home'),

                MenuSection::make('Questionnaire', [
                    MenuItem::resource(QuestionResource::class),
                    MenuItem::resource(QuestionGroupResource::class),
                    MenuItem::resource(CategoryResource::class),
                    MenuItem::resource(SessionResource::class),
                    MenuItem::resource(ScreeningResource::class),
                ])->icon('clipboard-document-list')->collapsible(),

                MenuSection::make('Logs', [
                    MenuItem::resource(LogResource::class),
                ])->icon('chart-bar')->collapsible(),

                MenuSection::make('Users', [
                    MenuItem::resource(\App\Nova\User::class),
                ])->icon('users')->collapsible(),

                MenuSection::make('Settings', [
                    MenuItem::resource(ConfigResource::class),
                ])->icon('cog')->collapsible(),
            ];
        });
    }

    /**
     * Register the configurations for Laravel Fortify.
     */
    protected function fortify(): void
    {
        Nova::fortify()
            ->features([
                Features::updatePasswords(),
                Features::emailVerification(),
                Features::twoFactorAuthentication(['confirm' => true, 'confirmPassword' => true]),
            ])
            ->register();
    }

    /**
     * Register the Nova routes.
     */
    protected function routes(): void
    {
        Nova::routes()
            ->withAuthenticationRoutes(default: false)
            ->withPasswordResetRoutes()
            ->withEmailVerificationRoutes()
            ->register();
    }

    /**
     * Register the Nova gate.
     *
     * This gate determines who can access Nova in non-local environments.
     */
    protected function gate(): void
    {
        Gate::define('viewNova', function (User $user) {
            return $user->role?->name === 'admin';
        });
    }

    /**
     * Get the dashboards that should be listed in the Nova sidebar.
     *
     * @return array<int, \Laravel\Nova\Dashboard>
     */
    protected function dashboards(): array
    {
        return [
            new \App\Nova\Dashboards\Main,
        ];
    }

    /**
     * Get the tools that should be listed in the Nova sidebar.
     *
     * @return array<int, \Laravel\Nova\Tool>
     */
    public function tools(): array
    {
        return [];
    }

    /**
     * Register any application services.
     */
    public function register(): void
    {
        parent::register();

        //
    }
}
