<?php

namespace Modules\SystemCore\App\Providers;

use App\Traits\RegistersMenuItems;
use Illuminate\Support\ServiceProvider;

class SystemCoreServiceProvider extends ServiceProvider
{
    use RegistersMenuItems;

    protected string $moduleName = 'SystemCore';

    protected string $moduleNameLower = 'systemcore';

    /**
     * Boot the application events.
     */
    public function boot(): void
    {
        $this->registerCommands();
        $this->registerCommandSchedules();
        $this->registerTranslations();
        $this->registerConfig();
        $this->registerViews();
        $this->loadMigrationsFrom(module_path($this->moduleName, 'Database/migrations'));

        // Register dynamic menu items
        $this->registerMenuItems();
    }

    /**
     * Register the service provider.
     */
    public function register(): void
    {
        $this->app->register(RouteServiceProvider::class);
    }

    /**
     * Register commands in the format of Command::class
     */
    protected function registerCommands(): void
    {
        // $this->commands([]);
    }

    /**
     * Register command Schedules.
     */
    protected function registerCommandSchedules(): void
    {
        // $this->app->booted(function () {
        //     $schedule = $this->app->make(Schedule::class);
        //     $schedule->command('inspire')->hourly();
        // });
    }

    /**
     * Register translations.
     */
    public function registerTranslations(): void
    {
        $langPath = resource_path('lang/modules/'.$this->moduleNameLower);

        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, $this->moduleNameLower);
            $this->loadJsonTranslationsFrom($langPath);
        } else {
            $this->loadTranslationsFrom(module_path($this->moduleName, 'resources/lang'), $this->moduleNameLower);
            $this->loadJsonTranslationsFrom(module_path($this->moduleName, 'resources/lang'));
        }
    }

    /**
     * Register config.
     */
    protected function registerConfig(): void
    {
        $this->publishes([module_path($this->moduleName, 'config/config.php') => config_path($this->moduleNameLower.'.php')], 'config');
        $this->mergeConfigFrom(module_path($this->moduleName, 'config/config.php'), $this->moduleNameLower);
    }

    /**
     * Register views.
     */
    public function registerViews(): void
    {
        $viewPath = resource_path('views/modules/'.$this->moduleNameLower);
        $sourcePath = module_path($this->moduleName, 'resources/views');

        $this->publishes([$sourcePath => $viewPath], ['views', $this->moduleNameLower.'-module-views']);

        $this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower);

        $componentNamespace = str_replace('/', '\\', config('modules.namespace').'\\'.$this->moduleName.'\\'.ltrim(config('modules.paths.generator.component-class.path'), config('modules.paths.app_folder', '')));
        \Illuminate\Support\Facades\Blade::componentNamespace($componentNamespace, $this->moduleNameLower);
    }

    /**
     * Get the services provided by the provider.
     */
    public function provides(): array
    {
        return [];
    }

    private function getPublishableViewPaths(): array
    {
        $paths = [];
        foreach (config('view.paths') as $path) {
            if (is_dir($path.'/modules/'.$this->moduleNameLower)) {
                $paths[] = $path.'/modules/'.$this->moduleNameLower;
            }
        }

        return $paths;
    }

    /**
     * Get the menu items for this module.
     *
     * @return array<array<string, mixed>>
     */
    protected function getMenuItems(): array
    {
        return [
            [
                'name' => 'Operations',
                'icon' => 'menu-icon bx bx-cog',
                'roles' => ['super_admin', 'admin', 'hr', 'hr_manager', 'hr_executive'],
                'slug' => [
                    'systemcore.dashboard',
                    'systemcore.customers.index',
                    'systemcore.suppliers.index',
                    'systemcore.products.index',
                    'systemcore.payment-terms.index',
                    'systemcore.tax-configurations.index',
                    'systemcore.product-categories.index',
                    'systemcore.sync.index',
                    'systemcore.settings.index',
                ],
                'submenu' => [
                    [
                        'url' => '/systemcore',
                        'name' => 'Dashboard',
                        'slug' => 'systemcore.dashboard',
                    ],
                    [
                        'name' => 'Master Data',
                        'slug' => [
                            'systemcore.customers.index',
                            'systemcore.suppliers.index',
                            'systemcore.products.index',
                        ],
                        'submenu' => [
                            [
                                'url' => '/systemcore/customers',
                                'name' => 'Customers',
                                'slug' => 'systemcore.customers.index',
                            ],
                            [
                                'url' => '/systemcore/suppliers',
                                'name' => 'Suppliers',
                                'slug' => 'systemcore.suppliers.index',
                            ],
                            [
                                'url' => '/systemcore/products',
                                'name' => 'Products',
                                'slug' => 'systemcore.products.index',
                            ],
                        ],
                    ],
                    [
                        'name' => 'Configuration',
                        'slug' => [
                            'systemcore.payment-terms.index',
                            'systemcore.tax-configurations.index',
                            'systemcore.product-categories.index',
                        ],
                        'submenu' => [
                            [
                                'url' => '/systemcore/payment-terms',
                                'name' => 'Payment Terms',
                                'slug' => 'systemcore.payment-terms.index',
                            ],
                            [
                                'url' => '/systemcore/tax-configurations',
                                'name' => 'Tax Configurations',
                                'slug' => 'systemcore.tax-configurations.index',
                            ],
                            [
                                'url' => '/systemcore/product-categories',
                                'name' => 'Product Categories',
                                'slug' => 'systemcore.product-categories.index',
                            ],
                        ],
                    ],
                    [
                        'url' => '/systemcore/sync',
                        'name' => 'Posting History',
                        'slug' => 'systemcore.sync.index',
                    ],
                    [
                        'url' => '/systemcore/settings',
                        'name' => 'Settings',
                        'slug' => 'systemcore.settings.index',
                    ],
                ],
            ],
        ];
    }

    /**
     * Get the menu priority for this module.
     * Lower values appear higher in the menu.
     */
    protected function getMenuPriority(): int
    {
        return 20; // Operations priority
    }
}
