<?php

namespace Modules\Admin;

use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Livewire\Livewire;
use Spatie\Permission\PermissionServiceProvider;
use Modules\Admin\App\Http\Middleware\AdminTemplateMiddleware;
use Modules\Admin\App\Listeners\ClearUserCache;
use Modules\Admin\App\Listeners\HandleUserLogin;
use Modules\Admin\App\Livewire\Cache\CacheFunctions;
use Modules\Admin\App\Livewire\Users\UserCount;
use Modules\Admin\App\Livewire\Users\UserTable;
use Modules\Admin\App\Livewire\Rbac\RoleCards;
use Modules\Admin\App\Livewire\Rbac\PermissionsIndex;
use Modules\Admin\App\Livewire\Cache\CacheStats;
use Modules\Admin\App\Livewire\Cache\SessionStats;
use Modules\Admin\App\Livewire\Cache\MemcachedStats;
use Modules\Admin\App\Livewire\Cache\RedisStats;
use Modules\Admin\App\Livewire\AdminSettings\ApplicationSettings;
use Modules\Admin\App\Livewire\AdminSettings\GeneralSettings;
use Modules\Admin\App\Livewire\AdminSettings\InterfaceSettings;
use Modules\Admin\App\Livewire\AdminSettings\MailSmtpSettings;
use Modules\Admin\App\Livewire\AdminSettings\MailSenderResponseSettings;
use Modules\Admin\App\Livewire\WebsiteSettings\AnalyticsSettings;
use Modules\Admin\App\Livewire\WebsiteSettings\ChatSettings;
use Modules\Admin\App\Livewire\WebsiteSettings\ContactFormSettings;
use Modules\Admin\App\Livewire\WebsiteSettings\ContactInfoSettings;
use Modules\Admin\App\Livewire\WebsiteSettings\FaviconSettings;
use Modules\Admin\App\Livewire\WebsiteSettings\ImageLogoSettings;
use Modules\Admin\App\Livewire\WebsiteSettings\LocationSettings;
use Modules\Admin\App\Livewire\WebsiteSettings\SocialSettings;
use Modules\Admin\App\Livewire\WebsiteSettings\TemplateSettings;
use Modules\Admin\App\Livewire\WebsiteSettings\WebsiteSettings;
use Modules\Admin\App\Livewire\WebsiteSettings\LegalSettings;
use Modules\Admin\App\Models\User;
use Modules\Admin\App\Providers\FortifyServiceProvider;
use Modules\Admin\App\Providers\GlobalSettingsProvider;
use Modules\Admin\App\Providers\VuexyAdminProvider;
use OwenIt\Auditing\AuditableObserver;

class ModuleServiceProvider extends ServiceProvider
{
    public static $prefixRoute = 'admin';

    public function register()
    {
        // Register the module's services and providers
        $this->app->register(FortifyServiceProvider::class);
        $this->app->register(PermissionServiceProvider::class);
        $this->app->register(GlobalSettingsProvider::class);
        $this->app->register(VuexyAdminProvider::class);
    }


    public function boot()
    {
        // Registrar alias del middleware
        $this->app['router']->aliasMiddleware('admin.settings', AdminTemplateMiddleware::class);

        // Register the module's routes
        Route::middleware(['web'])->group(__DIR__ . '/routes.php');

        // Register the module's views and resources
        $this->loadViewsFrom(__DIR__ . '/Resources/views', 'admin');

        // Register the migrations
        $this->loadMigrationsFrom(__DIR__ . '/Database/migrations');

        // Registrar manualmente  listeners
        Event::listen(Login::class, HandleUserLogin::class);
        Event::listen(Logout::class, ClearUserCache::class);

        // Register the module's Livewire components
        Livewire::component('user-count', UserCount::class);
        Livewire::component('user-table', UserTable::class);
        Livewire::component('role-card', RoleCards::class);
        Livewire::component('permissions-index', PermissionsIndex::class);

        Livewire::component('general-settings', GeneralSettings::class);
        Livewire::component('application-settings', ApplicationSettings::class);
        Livewire::component('interface-settings', InterfaceSettings::class);

        Livewire::component('cache-stats', CacheStats::class);
        Livewire::component('session-stats', SessionStats::class);
        Livewire::component('redis-stats', RedisStats::class);
        Livewire::component('memcached-stats', MemcachedStats::class);
        Livewire::component('cache-functions', CacheFunctions::class);

        Livewire::component('mail-smtp-settings', MailSmtpSettings::class);
        Livewire::component('mail-sender-response-settings', MailSenderResponseSettings::class);

        Livewire::component('website-settings', WebsiteSettings::class);
        Livewire::component('website-favicon-settings', FaviconSettings::class);
        Livewire::component('website-image-logo-settings', ImageLogoSettings::class);
        Livewire::component('website-social-settings', SocialSettings::class);
        Livewire::component('website-contact-form-settings', ContactFormSettings::class);
        Livewire::component('website-contact-info-settings', ContactInfoSettings::class);
        Livewire::component('website-location-settings', LocationSettings::class);
        Livewire::component('website-chat-settings', ChatSettings::class);
        Livewire::component('website-analytics-settings', AnalyticsSettings::class);
        Livewire::component('website-template-settings', TemplateSettings::class);
        Livewire::component('website-legal-settings', LegalSettings::class);

        // Enable auditing
        User::observe(AuditableObserver::class);
    }
}
