<?php

use Illuminate\Support\Facades\Route;
use App\Livewire\Asociado\FormularioSolicitud;
use App\Livewire\Asociado\MisSolicitudes;
use App\Livewire\Asociado\InformeMensualForm;
use App\Livewire\Evaluador\ListaSolicitudes;
use App\Livewire\Evaluador\EvaluarSolicitud;
use App\Livewire\Admin\Dashboard;
use App\Livewire\Admin\GestionDesembolsos;
use App\Livewire\Admin\Configuracion;
use App\Livewire\Admin\GestionUsuarios;
use App\Livewire\Junta\AprobacionJunta;

Route::get('/', function () {
    if (auth()->check()) return redirect()->route('dashboard'); return redirect()->route('login');
});

// ── Redirigir dashboard según rol ────────────────────────────
Route::middleware(['auth', 'verified'])->get('/dashboard', function () {
    $user = auth()->user();
    if ($user->hasRole('admin'))     return redirect()->route('admin.dashboard');
    if ($user->hasRole('evaluador')) return redirect()->route('evaluador.lista');
    if ($user->hasRole('junta'))     return redirect()->route('junta.aprobaciones');
    return redirect()->route('asociado.mis-solicitudes');
})->name('dashboard');

// ── Portal Asociado ───────────────────────────────────────────
Route::middleware(['auth', 'verified', 'role:asociado|admin'])
    ->prefix('asociado')
    ->name('asociado.')
    ->group(function () {
        Route::get('/mis-solicitudes',          MisSolicitudes::class)->name('mis-solicitudes');
        Route::get('/nueva-solicitud',          FormularioSolicitud::class)->name('nueva');
        Route::get('/solicitud/{id}/editar',    FormularioSolicitud::class)->name('editar');
        Route::get('/solicitud/{solicitud}/informe', InformeMensualForm::class)->name('informe');
    });

// ── Portal Evaluador ──────────────────────────────────────────
Route::middleware(['auth', 'verified', 'role:evaluador|admin'])
    ->prefix('evaluador')
    ->name('evaluador.')
    ->group(function () {
        Route::get('/solicitudes',              ListaSolicitudes::class)->name('lista');
        Route::get('/solicitud/{solicitud}',    EvaluarSolicitud::class)->name('evaluar');
    });

// ── Panel Administrativo ──────────────────────────────────────
Route::middleware(['auth', 'verified', 'role:admin'])
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {
        Route::get('/dashboard',    Dashboard::class)->name('dashboard');
        Route::get('/desembolsos',     GestionDesembolsos::class)->name('desembolsos');
        Route::get('/configuracion', Configuracion::class)->name('configuracion');
        Route::get('/usuarios',      GestionUsuarios::class)->name('usuarios');
    });

// ── Descarga de documentos privados (admin y evaluador) ───────
Route::middleware(['auth', 'verified', 'role:admin|evaluador'])
    ->get('/documento/{documento}/descargar', function (\App\Models\DocumentoAdjunto $documento) {
        return \Storage::disk('private')->download(
            $documento->ruta_archivo,
            $documento->nombre_original
        );
    })->name('admin.documento.descargar');

// ── Portal Junta Directiva ───────────────────────────────────
Route::middleware(['auth', 'verified', 'role:junta|admin'])
    ->prefix('junta')
    ->name('junta.')
    ->group(function () {
        Route::get('/aprobaciones', AprobacionJunta::class)->name('aprobaciones');
    });

// ── Jetstream routes ──────────────────────────────────────────
Route::middleware([
    'auth:sanctum',
    config('jetstream.auth_session'),
    'verified',
])->group(function () {
    // perfil de usuario Jetstream
});