<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Redirect;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return Redirect::route('login');
});

Auth::routes(['register' => false]); // Solo una vez

Route::middleware(['auth'])->group(function () {
    Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
    Route::get('/users', [UserController::class, 'index'])->name('users.index');
    Route::get('/users/update-data', [UserController::class, 'triggerDataUpdate'])->name('users.updateData');
    Route::get('/users/export', [UserController::class, 'export'])->name('users.export');
    Route::get('/users/{id}', [UserController::class, 'show'])->name('users.show');

    // Gestión de usuarios solo para Super Admin
    Route::middleware('can:manage-users')->group(function () {
        Route::get('/user-management', [App\Http\Controllers\UserManagementController::class, 'index'])->name('user-management.index');
        Route::get('/user-management/create', [App\Http\Controllers\UserManagementController::class, 'create'])->name('user-management.create');
        Route::post('/user-management', [App\Http\Controllers\UserManagementController::class, 'store'])->name('user-management.store');
        Route::get('/user-management/{id}/edit', [App\Http\Controllers\UserManagementController::class, 'edit'])->name('user-management.edit');
        Route::put('/user-management/{id}', [App\Http\Controllers\UserManagementController::class, 'update'])->name('user-management.update');
        Route::delete('/user-management/{id}', [App\Http\Controllers\UserManagementController::class, 'destroy'])->name('user-management.destroy');
    });
});
