<?php
use Illuminate\Support\Facades\Route;
use App\Models\Proceso;
use App\Models\CasoPrejuridico;
use App\Http\Controllers\OrdenGestionPdfController;
use App\Http\Controllers\PlantillaController;
Route::view('/', 'welcome');
Route::middleware(['auth', 'verified'])->group(function () {
    Route::view('dashboard', 'dashboard')->name('dashboard');
    Route::get('profile', [\App\Http\Controllers\ProfileController::class, 'edit'])->name('profile');
    Route::patch('profile', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update');
    Route::delete('profile', [\App\Http\Controllers\ProfileController::class, 'destroy'])->name('profile.destroy');
    Route::get('usuarios', function () {
        if (!auth()->user()->can('users.index')) { abort(403); }
        return view('users.index');
    })->name('usuarios.index');
    Route::get('clientes', function () {
        if (!auth()->user()->can('clientes.index')) { abort(403); }
        return view('clientes.index');
    })->name('clientes.index');
    Route::get('deudores', function () {
        if (!auth()->user()->can('deudores.index')) { abort(403); }
        return view('deudores.index');
    })->name('deudores.index');

    Route::get('deudores/{deudor}', function (\App\Models\Deudor $deudor) {
        if (!auth()->user()->can('deudores.index')) { abort(403); }
        return view('deudores.show', ['deudorId' => $deudor->id]);
    })->name('deudores.show');
    // === PROCESOS ===
    Route::get('procesos', function () {
        if (!auth()->user()->can('procesos.index')) { abort(403); }
        return view('procesos.index');
    })->name('procesos.index');
    Route::get('procesos/{proceso}', function (Proceso $proceso) {
        if (!auth()->user()->can('procesos.show')) { abort(403); }
        return view('procesos.show', compact('proceso'));
    })->name('procesos.show');
    // === PDF Orden Gestión ===
    Route::get('procesos/{proceso}/orden-pdf', [OrdenGestionPdfController::class, 'generar'])
        ->name('orden-gestion.pdf');
    // === PLANTILLAS PDF ===
    Route::get('procesos/{proceso}/plantilla/{tipo}', [PlantillaController::class, 'generar'])
        ->name('plantilla.generar');
    Route::get('procesos/{proceso}/plantilla/{tipo}/descargar', [PlantillaController::class, 'descargar'])
        ->name('plantilla.descargar');
    // === CASOS PREJURIDICOS ===
    Route::get('casos-prejuridicos', function () {
        return view('casos-prejuridicos.index');
    })->name('casos-prejuridicos.index');
    Route::get('casos-prejuridicos/nuevo', function () {
        return view('casos-prejuridicos.create');
    })->name('casos-prejuridicos.create');
    Route::get('casos-prejuridicos/{casoPrejuridico}', function (CasoPrejuridico $casoPrejuridico) {
        return view('casos-prejuridicos.show', compact('casoPrejuridico'));
    })->name('casos-prejuridicos.show');
    // === ALERTAS ===
    Route::get('alertas', function () {
        return view('alertas.index');
    })->name('alertas.index');
    // === CALENDARIO TÉRMINOS ===
    Route::get('calendario', function () {
        return view('alertas.calendario');
    })->name('calendario.index');
    // === IMPORTADOR ===
    Route::get('importar', function () {
        if (!auth()->user()->can('importar.index')) { abort(403); }
        return view('import.index');
    })->name('importar.index');
    // === AUDITORÍA ===
    Route::get('auditoria', function () {
        if (!auth()->user()->hasRole('admin')) { abort(403); }
        return view('auditoria.index');
    })->name('auditoria.index');
});
require __DIR__.'/auth.php';
// Sprint M2: Configuración de plataforma (solo admin)
Route::middleware(['auth', 'verified', 'role:admin'])->group(function () {
    Route::get('configuracion', function () {
        return view('configuracion.index');
    })->name('configuracion.index');
});
// Logout route
Route::post('/logout', function () {
    auth()->guard('web')->logout();
    request()->session()->invalidate();
    request()->session()->regenerateToken();
    return redirect('/');
})->middleware('auth')->name('logout');
// Raíz — redirige a dashboard o login
Route::get('/', function () {
    return auth()->check() ? redirect('/dashboard') : redirect('/login');
});
