<?php

    namespace Modules\Lpj\Providers;

    use Illuminate\Support\Facades\Blade;
    use Illuminate\Support\ServiceProvider;
    use Modules\Lpj\Models\BankData;
    use Modules\Lpj\Services\BankDataService;

    class LpjServiceProvider extends ServiceProvider
    {
        protected string $moduleName = 'Lpj';

        protected string $moduleNameLower = 'lpj';

        /**
         * 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'));

            if (class_exists('Breadcrumbs')) {
                require __DIR__ . '/../../routes/breadcrumbs.php';
            }
        }

        /**
     * Register commands in the format of Command::class
     */
    protected function registerCommands()
    : void
    {
        $this->commands([
            \Modules\Lpj\Console\Commands\CleanupInspeksiDataCommand::class,
            \Modules\Lpj\Console\Commands\CleanupSingleInspeksiCommand::class,
            \Modules\Lpj\Console\Commands\CleanupInspeksiStatusCommand::class,
        ]);
    }

        /**
     * Register command Schedules.
     */
    protected function registerCommandSchedules()
    : void
    {
        $this->app->booted(function () {
            $schedule = $this->app->make(\Illuminate\Console\Scheduling\Schedule::class);
            
            // Jalankan cleanup inspeksi setiap hari jam 2 pagi
            $schedule->command('lpj:cleanup-inspeksi --force')
                ->dailyAt('02:00')
                ->withoutOverlapping()
                ->onOneServer()
                ->runInBackground()
                ->appendOutputTo(storage_path('logs/cleanup-inspeksi.log'));
                
            // Backup cleanup setiap minggu
            $schedule->command('lpj:cleanup-inspeksi --force')
                ->weekly()
                ->sundays()
                ->at('03:00')
                ->withoutOverlapping()
                ->onOneServer()
                ->runInBackground()
                ->appendOutputTo(storage_path('logs/cleanup-inspeksi-weekly.log'));
        });
    }

        /**
         * 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, 'lang'), $this->moduleNameLower);
                $this->loadJsonTranslationsFrom(module_path($this->moduleName, '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', '')));
            Blade::componentNamespace($componentNamespace, $this->moduleNameLower);
        }

        /**
         * @return array<string>
         */
        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;
        }

        /**
         * Register the service provider.
         */
        public function register()
        : void
        {
            $this->app->register(EventServiceProvider::class);
            $this->app->register(RouteServiceProvider::class);
            $this->app->bind(BankDataService::class, function ($app){
                return new BankDataService($app->make(BankData::class));
            });
        }

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