<?php

/*
|--------------------------------------------------------------------------
| ROUTES MODULARI NETGESCON - SUPER ADMIN
|--------------------------------------------------------------------------
| 
| Tutte le route per il pannello super amministratore.
| Accesso completo al sistema, gestione globale e configurazioni.
|
| Autore: NetGesCon Development Team
| Data: 2024
|
*/

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SuperAdmin\DashboardController as SuperAdminDashboardController;
use App\Http\Controllers\SuperAdmin\UsersController as SuperAdminUsersController;
use App\Http\Controllers\SuperAdmin\AmministratoriController;
use App\Http\Controllers\SuperAdmin\SystemController;
use App\Http\Controllers\SuperAdmin\SettingsController as SuperAdminSettingsController;
use App\Http\Controllers\SuperAdmin\BackupController;
use App\Http\Controllers\SuperAdmin\LogsController;
use App\Http\Controllers\SuperAdmin\MenuPermissionController;
use App\Http\Controllers\SuperAdmin\ThemeController;

/*
|--------------------------------------------------------------------------
| GRUPPO SUPER ADMIN - Middleware e Prefix
|--------------------------------------------------------------------------
*/
Route::middleware(['auth', 'verified', 'role:super-admin'])
    ->prefix('superadmin')
    ->name('superadmin.')
    ->group(function () {

    /*
    |--------------------------------------------------------------------------
    | DASHBOARD SUPER ADMIN
    |--------------------------------------------------------------------------
    */
    Route::get('/', [SuperAdminDashboardController::class, 'index'])->name('dashboard');
    Route::get('/dashboard', [SuperAdminDashboardController::class, 'index'])->name('dashboard.main');
    
    // API per dashboard super admin
    Route::prefix('api/dashboard')->name('dashboard.api.')->group(function () {
        Route::get('/system-stats', [SuperAdminDashboardController::class, 'getSystemStats'])->name('system.stats');
        Route::get('/user-activity', [SuperAdminDashboardController::class, 'getUserActivity'])->name('user.activity');
        Route::get('/performance', [SuperAdminDashboardController::class, 'getPerformance'])->name('performance');
        Route::get('/security-overview', [SuperAdminDashboardController::class, 'getSecurityOverview'])->name('security');
    });

    /*
    |--------------------------------------------------------------------------
    | GESTIONE GLOBALE UTENTI
    |--------------------------------------------------------------------------
    */
    Route::prefix('users')->name('users.')->group(function () {
        Route::get('/', [SuperAdminUsersController::class, 'index'])->name('index');
        Route::get('/create', [SuperAdminUsersController::class, 'create'])->name('create');
        Route::post('/', [SuperAdminUsersController::class, 'store'])->name('store');
        Route::get('/{user}', [SuperAdminUsersController::class, 'show'])->name('show');
        Route::get('/{user}/edit', [SuperAdminUsersController::class, 'edit'])->name('edit');
        Route::put('/{user}', [SuperAdminUsersController::class, 'update'])->name('update');
        Route::delete('/{user}', [SuperAdminUsersController::class, 'destroy'])->name('destroy');
        
        // Gestione avanzata utenti
        Route::post('/{user}/suspend', [SuperAdminUsersController::class, 'suspend'])->name('suspend');
        Route::post('/{user}/activate', [SuperAdminUsersController::class, 'activate'])->name('activate');
        Route::post('/{user}/impersonate', [SuperAdminUsersController::class, 'impersonate'])->name('impersonate');
        Route::post('/stop-impersonation', [SuperAdminUsersController::class, 'stopImpersonation'])->name('stop-impersonation');
        
        // Gestione ruoli e permessi globali
        Route::get('/{user}/permissions', [SuperAdminUsersController::class, 'permissions'])->name('permissions');
        Route::post('/{user}/permissions', [SuperAdminUsersController::class, 'updatePermissions'])->name('permissions.update');
        Route::post('/{user}/roles/assign', [SuperAdminUsersController::class, 'assignRole'])->name('roles.assign');
        Route::delete('/{user}/roles/{role}', [SuperAdminUsersController::class, 'removeRole'])->name('roles.remove');
        
        // Azioni bulk
        Route::post('/bulk/delete', [SuperAdminUsersController::class, 'bulkDelete'])->name('bulk.delete');
        Route::post('/bulk/suspend', [SuperAdminUsersController::class, 'bulkSuspend'])->name('bulk.suspend');
        Route::post('/bulk/activate', [SuperAdminUsersController::class, 'bulkActivate'])->name('bulk.activate');
        Route::post('/bulk/export', [SuperAdminUsersController::class, 'bulkExport'])->name('bulk.export');
    });

    /*
    |--------------------------------------------------------------------------
    | GESTIONE AMMINISTRATORI
    |--------------------------------------------------------------------------
    */
    Route::prefix('amministratori')->name('amministratori.')->group(function () {
        Route::get('/', [AmministratoriController::class, 'index'])->name('index');
        Route::get('/create', [AmministratoriController::class, 'create'])->name('create');
        Route::post('/', [AmministratoriController::class, 'store'])->name('store');
        Route::get('/{amministratore}', [AmministratoriController::class, 'show'])->name('show');
        Route::get('/{amministratore}/edit', [AmministratoriController::class, 'edit'])->name('edit');
        Route::put('/{amministratore}', [AmministratoriController::class, 'update'])->name('update');
        Route::delete('/{amministratore}', [AmministratoriController::class, 'destroy'])->name('destroy');
        
        // Gestione stabili assegnati
        Route::get('/{amministratore}/stabili', [AmministratoriController::class, 'stabili'])->name('stabili');
        Route::post('/{amministratore}/stabili/assign', [AmministratoriController::class, 'assignStabile'])->name('stabili.assign');
        Route::delete('/{amministratore}/stabili/{stabile}', [AmministratoriController::class, 'removeStabile'])->name('stabili.remove');
        
        // Performance e statistiche
        Route::get('/{amministratore}/performance', [AmministratoriController::class, 'performance'])->name('performance');
        Route::get('/{amministratore}/reports', [AmministratoriController::class, 'reports'])->name('reports');
    });

    /*
    |--------------------------------------------------------------------------
    | GESTIONE SISTEMA
    |--------------------------------------------------------------------------
    */
    Route::prefix('system')->name('system.')->group(function () {
        Route::get('/', [SystemController::class, 'index'])->name('index');
        
        // Monitoraggio sistema
        Route::get('/monitor', [SystemController::class, 'monitor'])->name('monitor');
        Route::get('/health', [SystemController::class, 'health'])->name('health');
        Route::get('/performance', [SystemController::class, 'performance'])->name('performance');
        Route::get('/security', [SystemController::class, 'security'])->name('security');
        
        // Gestione database
        Route::prefix('database')->name('database.')->group(function () {
            Route::get('/', [SystemController::class, 'databaseInfo'])->name('info');
            Route::post('/optimize', [SystemController::class, 'optimizeDatabase'])->name('optimize');
            Route::post('/repair', [SystemController::class, 'repairDatabase'])->name('repair');
            Route::get('/migrations', [SystemController::class, 'migrations'])->name('migrations');
            Route::post('/migrate', [SystemController::class, 'runMigrations'])->name('migrate');
        });
        
        // Gestione cache
        Route::prefix('cache')->name('cache.')->group(function () {
            Route::get('/', [SystemController::class, 'cacheInfo'])->name('info');
            Route::post('/clear', [SystemController::class, 'clearCache'])->name('clear');
            Route::post('/config/clear', [SystemController::class, 'clearConfigCache'])->name('config.clear');
            Route::post('/route/clear', [SystemController::class, 'clearRouteCache'])->name('route.clear');
            Route::post('/view/clear', [SystemController::class, 'clearViewCache'])->name('view.clear');
        });
        
        // Gestione queue e jobs
        Route::prefix('queue')->name('queue.')->group(function () {
            Route::get('/', [SystemController::class, 'queueInfo'])->name('info');
            Route::post('/restart', [SystemController::class, 'restartQueue'])->name('restart');
            Route::get('/failed', [SystemController::class, 'failedJobs'])->name('failed');
            Route::post('/retry/{id}', [SystemController::class, 'retryJob'])->name('retry');
        });
    });

    /*
    |--------------------------------------------------------------------------
    | GESTIONE MENU E PERMESSI
    |--------------------------------------------------------------------------
    */
    Route::prefix('menu-permissions')->name('menu.')->group(function () {
        Route::get('/', [MenuPermissionController::class, 'index'])->name('index');
        
        // Gestione menu
        Route::get('/menus', [MenuPermissionController::class, 'menus'])->name('menus');
        Route::post('/menus', [MenuPermissionController::class, 'createMenu'])->name('menus.create');
        Route::put('/menus/{menu}', [MenuPermissionController::class, 'updateMenu'])->name('menus.update');
        Route::delete('/menus/{menu}', [MenuPermissionController::class, 'deleteMenu'])->name('menus.delete');
        Route::post('/menus/reorder', [MenuPermissionController::class, 'reorderMenus'])->name('menus.reorder');
        
        // Gestione permessi per ruoli
        Route::get('/roles/{role}/permissions', [MenuPermissionController::class, 'rolePermissions'])->name('roles.permissions');
        Route::post('/roles/{role}/permissions', [MenuPermissionController::class, 'updateRolePermissions'])->name('roles.permissions.update');
        
        // Gestione permessi per utenti
        Route::get('/users/{user}/permissions', [MenuPermissionController::class, 'userPermissions'])->name('users.permissions');
        Route::post('/users/{user}/permissions', [MenuPermissionController::class, 'updateUserPermissions'])->name('users.permissions.update');
        
        // Template permessi
        Route::get('/templates', [MenuPermissionController::class, 'templates'])->name('templates');
        Route::post('/templates', [MenuPermissionController::class, 'createTemplate'])->name('templates.create');
        Route::post('/templates/{template}/apply', [MenuPermissionController::class, 'applyTemplate'])->name('templates.apply');
    });

    /*
    |--------------------------------------------------------------------------
    | GESTIONE TEMI E PERSONALIZZAZIONE
    |--------------------------------------------------------------------------
    */
    Route::prefix('themes')->name('themes.')->group(function () {
        Route::get('/', [ThemeController::class, 'index'])->name('index');
        
        // Gestione temi
        Route::get('/editor', [ThemeController::class, 'editor'])->name('editor');
        Route::post('/save', [ThemeController::class, 'saveTheme'])->name('save');
        Route::post('/reset', [ThemeController::class, 'resetTheme'])->name('reset');
        Route::post('/export', [ThemeController::class, 'exportTheme'])->name('export');
        Route::post('/import', [ThemeController::class, 'importTheme'])->name('import');
        
        // Gestione colori e stili
        Route::post('/colors', [ThemeController::class, 'updateColors'])->name('colors.update');
        Route::post('/fonts', [ThemeController::class, 'updateFonts'])->name('fonts.update');
        Route::post('/layout', [ThemeController::class, 'updateLayout'])->name('layout.update');
        
        // Template temi
        Route::get('/templates', [ThemeController::class, 'templates'])->name('templates');
        Route::post('/templates/{template}/apply', [ThemeController::class, 'applyTemplate'])->name('templates.apply');
    });

    /*
    |--------------------------------------------------------------------------
    | BACKUP E RIPRISTINO
    |--------------------------------------------------------------------------
    */
    Route::prefix('backup')->name('backup.')->group(function () {
        Route::get('/', [BackupController::class, 'index'])->name('index');
        
        // Gestione backup
        Route::post('/create', [BackupController::class, 'create'])->name('create');
        Route::get('/{backup}/download', [BackupController::class, 'download'])->name('download');
        Route::delete('/{backup}', [BackupController::class, 'delete'])->name('delete');
        Route::post('/{backup}/restore', [BackupController::class, 'restore'])->name('restore');
        
        // Configurazione backup automatici
        Route::get('/settings', [BackupController::class, 'settings'])->name('settings');
        Route::post('/settings', [BackupController::class, 'updateSettings'])->name('settings.update');
        Route::post('/schedule/test', [BackupController::class, 'testSchedule'])->name('schedule.test');
        
        // Backup cloud
        Route::prefix('cloud')->name('cloud.')->group(function () {
            Route::get('/', [BackupController::class, 'cloudIndex'])->name('index');
            Route::post('/upload', [BackupController::class, 'uploadToCloud'])->name('upload');
            Route::post('/sync', [BackupController::class, 'syncWithCloud'])->name('sync');
            Route::get('/providers', [BackupController::class, 'cloudProviders'])->name('providers');
        });
    });

    /*
    |--------------------------------------------------------------------------
    | LOGS E DEBUGGING
    |--------------------------------------------------------------------------
    */
    Route::prefix('logs')->name('logs.')->group(function () {
        Route::get('/', [LogsController::class, 'index'])->name('index');
        
        // Visualizzazione logs
        Route::get('/application', [LogsController::class, 'application'])->name('application');
        Route::get('/system', [LogsController::class, 'system'])->name('system');
        Route::get('/security', [LogsController::class, 'security'])->name('security');
        Route::get('/errors', [LogsController::class, 'errors'])->name('errors');
        
        // Gestione logs
        Route::post('/clear', [LogsController::class, 'clear'])->name('clear');
        Route::get('/download', [LogsController::class, 'download'])->name('download');
        Route::get('/search', [LogsController::class, 'search'])->name('search');
        
        // Analisi logs
        Route::get('/analytics', [LogsController::class, 'analytics'])->name('analytics');
        Route::get('/patterns', [LogsController::class, 'patterns'])->name('patterns');
        Route::get('/alerts', [LogsController::class, 'alerts'])->name('alerts');
        Route::post('/alerts', [LogsController::class, 'createAlert'])->name('alerts.create');
    });

    /*
    |--------------------------------------------------------------------------
    | IMPOSTAZIONI GLOBALI
    |--------------------------------------------------------------------------
    */
    Route::prefix('settings')->name('settings.')->group(function () {
        Route::get('/', [SuperAdminSettingsController::class, 'index'])->name('index');
        
        // Configurazioni generali
        Route::get('/general', [SuperAdminSettingsController::class, 'general'])->name('general');
        Route::post('/general', [SuperAdminSettingsController::class, 'updateGeneral'])->name('general.update');
        
        // Configurazioni sicurezza
        Route::get('/security', [SuperAdminSettingsController::class, 'security'])->name('security');
        Route::post('/security', [SuperAdminSettingsController::class, 'updateSecurity'])->name('security.update');
        
        // Configurazioni email
        Route::get('/email', [SuperAdminSettingsController::class, 'email'])->name('email');
        Route::post('/email', [SuperAdminSettingsController::class, 'updateEmail'])->name('email.update');
        Route::post('/email/test', [SuperAdminSettingsController::class, 'testEmail'])->name('email.test');
        
        // Configurazioni API
        Route::get('/api', [SuperAdminSettingsController::class, 'api'])->name('api');
        Route::post('/api', [SuperAdminSettingsController::class, 'updateApi'])->name('api.update');
        Route::post('/api/keys/generate', [SuperAdminSettingsController::class, 'generateApiKey'])->name('api.keys.generate');
        Route::delete('/api/keys/{key}', [SuperAdminSettingsController::class, 'revokeApiKey'])->name('api.keys.revoke');
        
        // Configurazioni integrazione
        Route::get('/integrations', [SuperAdminSettingsController::class, 'integrations'])->name('integrations');
        Route::post('/integrations', [SuperAdminSettingsController::class, 'updateIntegrations'])->name('integrations.update');
        
        // Manutenzione sistema
        Route::prefix('maintenance')->name('maintenance.')->group(function () {
            Route::get('/', [SuperAdminSettingsController::class, 'maintenance'])->name('index');
            Route::post('/mode/enable', [SuperAdminSettingsController::class, 'enableMaintenanceMode'])->name('enable');
            Route::post('/mode/disable', [SuperAdminSettingsController::class, 'disableMaintenanceMode'])->name('disable');
            Route::post('/optimize', [SuperAdminSettingsController::class, 'optimizeApplication'])->name('optimize');
            Route::post('/update/check', [SuperAdminSettingsController::class, 'checkUpdates'])->name('update.check');
        });
    });

}); // Fine gruppo super admin
