<?php

namespace App\Providers;

use App\Modules\GlobalOrder\Nova\Resources\GlobalOrderResource;
use App\Nova\Dashboards\Main;
use App\Nova\Resources\CMS\Forms\ContactUS;
use App\Nova\Resources\CMS\Marketing\FlashSale;
use App\Nova\Resources\CMS\Marketing\Newsletter;
use App\Nova\Resources\CMS\Marketing\NewsletterUser;
use App\Nova\Resources\CMS\Media\Banner;
use App\Nova\Resources\CMS\Media\Carousel;
use App\Nova\Resources\CMS\Media\Gallery;
use App\Nova\Resources\CMS\Media\Story;
use App\Nova\Resources\Ecommerce\Channel\Channel;
use App\Nova\Resources\Ecommerce\Payout\PayoutResource;
use App\Nova\Resources\Ecommerce\Product\Attribute\Attribute;
use App\Nova\Resources\Ecommerce\Product\Brand\Brand;
use App\Nova\Resources\Ecommerce\Product\Category\Category;
use App\Nova\Resources\Ecommerce\Product\Collection\Collection;
use App\Nova\Resources\Ecommerce\Product\Coupon\Coupon;
use App\Nova\Resources\Ecommerce\Product\Inventory\Inventory;
use App\Nova\Resources\Ecommerce\Product\Inventory\InventoryHistoryRemovedResource;
use App\Nova\Resources\Ecommerce\Product\Inventory\InventoryHistoryResource;
use App\Nova\Resources\Ecommerce\Product\Order\Order;
use App\Nova\Resources\Ecommerce\Product\Order\OrderShippingMethod;
use App\Nova\Resources\Ecommerce\Product\Product\Product;
use App\Nova\Resources\Ecommerce\Product\Review\Review;
use App\Nova\Resources\Legal\LegalPage;
use App\Nova\Resources\Post\PostBranch\PostBranch;
use App\Nova\Resources\System\Payments\PaymentType;
use App\Nova\Resources\System\Roles\Permission;
use App\Nova\Resources\System\Roles\Role;
use App\Nova\Resources\System\Settings\Location\Province\Province;
use App\Nova\Resources\System\VersionManagement\AppVersion;
use App\Nova\Seller\Seller;
use App\Nova\User;
use App\Repositories\Nova\NovaRepo;
use Eolica\NovaLocaleSwitcher\LocaleSwitcher;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Laravel\Nova\Events\ServingNova;
use Laravel\Nova\Fields\Field;
use Laravel\Nova\Menu\MenuGroup;
use Laravel\Nova\Menu\MenuItem;
use Laravel\Nova\Menu\MenuSection;
use Laravel\Nova\Nova;
use Laravel\Nova\NovaApplicationServiceProvider;

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

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

        Nova::withoutNotificationCenter();

        Nova::withBreadcrumbs(NovaRepo::breadcrumbs());
        Nova::footer(NovaRepo::footer());

        $this->setupNavigation();
        $this->setupUserSettings();
        $this->addAssets();
        $this->setupFieldMacros();
    }

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

    /**
     * Register the Nova gate.
     *
     * This gate determines who can access Nova in non-local environments.
     */
    protected function gate(): void
    {
        Gate::define('viewNova', NovaRepo::canViewNova());

        Gate::define('viewLogs', fn ($user) => $user->isMe());
        Gate::define('downloadLogs', fn ($user) => $user->isMe());
        Gate::define('deleteLogs', fn ($user) => $user->isMe());
    }

    /**
     * Get the dashboards that should be listed in the Nova sidebar.
     */
    protected function dashboards(): array
    {
        return [
            new Main,
        ];
    }

    /**
     * Get the tools that should be listed in the Nova sidebar.
     */
    public function tools(): array
    {
        return [
            LocaleSwitcher::make()
                ->setLocales(config('app.locales'))
                ->onSwitchLocale(NovaRepo::localeSwitcherSave()),
        ];
    }

    /**
     * Setup user settings
     */
    public function setupUserSettings(): void
    {
        Nova::serving(fn (ServingNova $event) => NovaRepo::serving($event));
    }

    /**
     * Setup navigation
     */
    public function setupNavigation(): void
    {
        Nova::mainMenu(function (Request $request) {
            return [
                MenuSection::dashboard(Main::class)->icon('chart-bar'),

                MenuSection::make(__('Orders'), [
                    MenuItem::resource(Order::class),
                ])->icon('shopping-cart')->collapsedByDefault(),

                MenuSection::make(__('E-commerce'), [
                    MenuSection::make(__('Products'), [
                        MenuItem::resource(Product::class),
                        MenuItem::resource(Review::class),
                    ])->icon('cube'),

                    MenuSection::make(__('Inventory'), [
                        MenuItem::resource(Inventory::class),
                        MenuItem::resource(InventoryHistoryResource::class),
                        MenuItem::resource(InventoryHistoryRemovedResource::class),
                    ])->icon('office-building'),

                    MenuSection::make(__('Category'), [
                        MenuItem::resource(Channel::class),
                        MenuItem::resource(Collection::class),
                        MenuItem::resource(Category::class),
                        MenuItem::resource(Brand::class),
                        MenuItem::resource(Attribute::class),
                    ])->icon('color-swatch'),
                ])->icon('shopping-bag')->collapsedByDefault(),

                MenuSection::make(__('Users'), [
                    MenuItem::resource(Seller::class),
                    MenuItem::resource(User::class),
                ])->icon('users')->collapsedByDefault(),

                MenuSection::make(__('Global orders'), [
                    MenuItem::resource(GlobalOrderResource::class),
                ])->icon('globe')->collapsedByDefault(),

                MenuSection::make(__('Payouts'), [
                    MenuItem::resource(PayoutResource::class),
                ])->icon('credit-card')->collapsedByDefault(),

                MenuSection::make(__('CMS'), [
                    MenuGroup::make(__('Media'), [
                        MenuItem::resource(Banner::class),
                        MenuItem::resource(Carousel::class),
                        MenuItem::resource(Gallery::class),
                        MenuItem::resource(Story::class),
                    ])->collapsedByDefault(),

                    MenuGroup::make(__('Legal'), [
                        MenuItem::resource(LegalPage::class),
                    ])->collapsedByDefault(),
                ])->icon('newspaper')->collapsedByDefault(),

                MenuSection::make(__('Marketing'), [
                    MenuItem::resource(FlashSale::class),
                    MenuItem::resource(NewsletterUser::class),
                    MenuItem::resource(Newsletter::class),
                    MenuItem::resource(Coupon::class),
                ])->icon('presentation-chart-bar')->collapsedByDefault(),

                MenuSection::make('Hatlar', [
                    MenuItem::resource(ContactUS::class),
                ])->icon('inbox-in')->collapsedByDefault(),

                MenuSection::make(__('System'), [
                    MenuGroup::make(__('User'), [
                        MenuItem::resource(Role::class),
                        MenuItem::resource(Permission::class),
                    ])->collapsedByDefault(),

                    MenuGroup::make(__('Location'), [
                        MenuItem::resource(Province::class),
                        MenuItem::resource(PostBranch::class),
                    ])->collapsedByDefault(),

                    MenuGroup::make(__('Payment types'), [
                        MenuItem::resource(PaymentType::class),
                    ])->collapsedByDefault(),

                    MenuGroup::make(__('Shipping methods'), [
                        MenuItem::resource(OrderShippingMethod::class),
                    ])->collapsedByDefault(),

                    MenuGroup::make(__('App'), [
                        MenuItem::resource(AppVersion::class),
                    ])->collapsedByDefault(),
                ])->icon('cog')->collapsedByDefault(),
            ];
        });
    }

    /**
     * Add custom assets
     */
    public function addAssets(): void
    {
        Nova::style('additional', public_path('assets/panel/css/additional.css'));
        Nova::script('additional', public_path('assets/panel/js/additional.js'));
    }

    /**
     * Setup macros of fields
     */
    public function setupFieldMacros(): void
    {
        Field::macro('defaultStringRules', function () {
            $this->rules('required', 'string', 'max:255');

            return $this;
        });

        Field::macro('phoneNumberRule', function () {
            $this->rules('required', 'integer', 'between:61000000,71999999');

            return $this;
        });

        Field::macro('turkmenDate', function () {
            $this->displayUsing(fn ($value) => $value?->format('H:i, d.m.Y'));

            return $this;
        });

        Field::macro('showWhen', function ($condition) {
            $condition === true ? $this->show() : $this->hide();

            return $this;
        });
    }

    /**
     * Register the application's Nova resources.
     */
    protected function resources(): void
    {
        Nova::resourcesIn(app_path('Nova'));
        Nova::resourcesIn(app_path('Modules'));
    }
}
