<?php

namespace Koneko\VuexyAdminModule;

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 Koneko\VuexyAdminModule\App\Http\Middleware\AdminTemplateMiddleware;
use Koneko\VuexyAdminModule\App\Listeners\ClearUserCache;
use Koneko\VuexyAdminModule\App\Listeners\HandleUserLogin;
use Koneko\VuexyAdminModule\App\Livewire\Cache\CacheFunctions;
use Koneko\VuexyAdminModule\App\Livewire\Users\UserCount;
use Koneko\VuexyAdminModule\App\Livewire\Users\UserTable;
use Koneko\VuexyAdminModule\App\Livewire\Rbac\RoleCards;
use Koneko\VuexyAdminModule\App\Livewire\Rbac\PermissionsIndex;
use Koneko\VuexyAdminModule\App\Livewire\Cache\CacheStats;
use Koneko\VuexyAdminModule\App\Livewire\Cache\SessionStats;
use Koneko\VuexyAdminModule\App\Livewire\Cache\MemcachedStats;
use Koneko\VuexyAdminModule\App\Livewire\Cache\RedisStats;
use Koneko\VuexyAdminModule\App\Livewire\AdminSettings\ApplicationSettings;
use Koneko\VuexyAdminModule\App\Livewire\AdminSettings\GeneralSettings;
use Koneko\VuexyAdminModule\App\Livewire\AdminSettings\InterfaceSettings;
use Koneko\VuexyAdminModule\App\Livewire\AdminSettings\MailSmtpSettings;
use Koneko\VuexyAdminModule\App\Livewire\AdminSettings\MailSenderResponseSettings;
use Koneko\VuexyAdminModule\App\Livewire\WebsiteSettings\AnalyticsSettings;
use Koneko\VuexyAdminModule\App\Livewire\WebsiteSettings\ChatSettings;
use Koneko\VuexyAdminModule\App\Livewire\WebsiteSettings\ContactFormSettings;
use Koneko\VuexyAdminModule\App\Livewire\WebsiteSettings\ContactInfoSettings;
use Koneko\VuexyAdminModule\App\Livewire\WebsiteSettings\FaviconSettings;
use Koneko\VuexyAdminModule\App\Livewire\WebsiteSettings\ImageLogoSettings;
use Koneko\VuexyAdminModule\App\Livewire\WebsiteSettings\LocationSettings;
use Koneko\VuexyAdminModule\App\Livewire\WebsiteSettings\SocialSettings;
use Koneko\VuexyAdminModule\App\Livewire\WebsiteSettings\TemplateSettings;
use Koneko\VuexyAdminModule\App\Livewire\WebsiteSettings\WebsiteSettings;
use Koneko\VuexyAdminModule\App\Livewire\WebsiteSettings\LegalSettings;
use Koneko\VuexyAdminModule\App\Models\User;
use Koneko\VuexyAdminModule\App\Providers\FortifyServiceProvider;
use Koneko\VuexyAdminModule\App\Providers\GlobalSettingsProvider;
use Koneko\VuexyAdminModule\App\Providers\VuexyAdminProvider;
use OwenIt\Auditing\AuditableObserver;

class BaseServiceProvider 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);
    }
}
