<?php

use Illuminate\Support\Facades\Route;
use Modules\Admin\App\Http\Controllers\AdminController;
use Modules\Admin\App\Http\Controllers\CacheController;
use Modules\Admin\App\Http\Controllers\HomeController;
use Modules\Admin\App\Http\Controllers\UserController;
use Modules\Admin\App\Http\Controllers\RoleController;
use Modules\Admin\App\Http\Controllers\PermissionController;
use Modules\Admin\App\Http\Controllers\WebsiteSettingController;

// Grupo raíz para admin con middleware y prefijos comunes
Route::prefix('admin')->name('admin.')->middleware(['auth', 'admin.settings'])->group(function () {
    // Rutas de HomeController
    Route::controller(HomeController::class)->group(function () {
        Route::get('/', 'index')->name('home.index');
        Route::get('acerca-de', 'about')->name('about.index');
        Route::get('muy-pronto', 'comingsoon')->name('comingsoon.index');
        Route::get('bajo-mantenimiento', 'underMaintenance')->name('under-maintenance.index');
    });

    // Rutas de UserController
    Route::controller(UserController::class)->prefix('usuario')->name('user.profile.')->group(function () {
        Route::get('perfil', 'edit')->name('edit');
        Route::patch('perfil', 'update')->name('update');
        Route::delete('perfil', 'destroy')->name('destroy');
        Route::get('avatar', 'generateAvatar')->name('avatar');
    });

    // Rutas de UserController
    Route::resource('sistema/usuarios', UserController::class)->names('system.users');

    Route::controller(UserController::class)->group(function () {
        Route::post('usuarios/usuarios/{user}/update', 'updateAjax')->name('system.users.update-ajax');
        Route::get('usuario/configuraciones-de-cuenta', 'userSettings')->name('system.user-settings.index');
    });


    // Rutas de RoleController
    Route::controller(RoleController::class)->prefix('sistema/rbac/roles')->name('system.roles.')->group(function () {
        Route::get('/', 'index')->name('index');
        Route::get('ajax/check-unique-name', 'checkUniqueRoleName')->name('check-unique-name');
    });

    // Rutas de PermissionController
    Route::controller(PermissionController::class)->prefix('sistema/rbac/permisos')->name('system.permissions.')->group(function () {
        Route::get('/', 'index')->name('index');
        Route::get('/ajax/check-unique-name', 'checkUniquePermissionName')->name('check-unique-name');
    });


    // Rutas de AdminController
    Route::controller(AdminController::class)->group(function () {
        Route::post('quicklinks-update', 'quickLinksUpdate')->name('system.quicklinks-navbar.update');
        Route::get('ajustes/aplicacion/ajustes-generales', 'generalSettings')->name('webapp.general-settings.index');
        Route::get('ajustes/aplicacion/servidor-de-correo-smtp', 'smtpSettings')->name('webapp.smtp-settings.index');
    });

    Route::controller(CacheController::class)->group(function () {
        Route::get('ajustes/aplicacion/ajustes-de-cache', 'cacheManager')->name('webapp.cache-manager.index');
        Route::post('cache/config/cache', 'generateConfigCache');
        Route::post('cache/route/cache', 'generateRouteCache');
    });

    // Rutas de WebsiteSettingController
    Route::controller(WebsiteSettingController::class)->group(function () {
        Route::get('ajustes/website/ajustes-generales', 'generalSettings')->name('website.general-settings.index');
        Route::get('ajustes/website/avisos-legales', 'legal')->name('website.legal.index');
    });
});

// Rutas públicas sin autenticación
Route::prefix('admin')->name('admin.')->group(function () {
    Route::get('search-navbar', [AdminController::class, 'searchNavbar'])->name('system.search-navbar.index');
});
