<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\TesoreriaController;
use App\Http\Controllers\SecretariaController;
use App\Http\Controllers\EstudianteController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\ServicioController;

/*
|--------------------------------------------------------------------------
| RUTAS PÚBLICAS
|--------------------------------------------------------------------------
*/

Route::get('/', function () {
    return view('welcome');
});

/*
|--------------------------------------------------------------------------
| DASHBOARD GENERAL
|--------------------------------------------------------------------------
*/

Route::middleware(['auth'])->group(function () {
    Route::get('/dashboard', function () {
        return view('dashboard');
    })->name('dashboard');
});

/*
|--------------------------------------------------------------------------
| PERFIL
|--------------------------------------------------------------------------
*/

Route::middleware(['auth'])->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])
        ->name('profile.edit');

    Route::patch('/profile', [ProfileController::class, 'update'])
        ->name('profile.update');

    Route::delete('/profile', [ProfileController::class, 'destroy'])
        ->name('profile.destroy');
});

/*
|--------------------------------------------------------------------------
| ADMIN
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'role:admin'])
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {

        // Dashboard Admin
        Route::get('/', [AdminController::class, 'index'])
            ->name('dashboard');

        // CRUD USUARIOS
        Route::resource('usuarios', UserController::class);

        // CRUD SERVICIOS
        Route::resource('servicios', ServicioController::class);

        // Admin: reportes, pagos, configuracion (vistas simples)
        Route::get('reportes', function () { return view('admin.reportes.index'); })->name('reportes.index');
        Route::get('pagos', function () { return view('admin.pagos.index'); })->name('pagos.index');
        Route::get('configuracion', function () { return view('admin.configuracion.index'); })->name('configuracion.index');
        // Admin: gestión de estudiantes
        Route::get('estudiantes', function () {
            $estudiantes = \App\Models\User::where('rol', 'estudiante')->get();
            return view('admin.estudiantes.index', compact('estudiantes'));
        })->name('estudiantes.index');
    });

/*
|--------------------------------------------------------------------------
| TESORERÍA
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'role:tesoreria'])
    ->prefix('tesoreria')
    ->name('tesoreria.')
    ->group(function () {

        Route::get('/', [TesoreriaController::class, 'index'])
            ->name('dashboard');

        // Tesorería: pagos realizados, generar boleta, deudas pendientes
        Route::get('pagos', function () {
            $pagos = \App\Models\Pago::all();
            return view('tesoreria.pagos.index', compact('pagos'));
        })->name('pagos');

        Route::get('boletas', function () {
            return view('tesoreria.boletas.index');
        })->name('boletas');

        Route::get('deudas', function () {
            $deudas = []; // placeholder: implementar consulta real
            return view('tesoreria.deudas.index', compact('deudas'));
        })->name('deudas');
    });

/*
|--------------------------------------------------------------------------
| SECRETARÍA
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'role:secretaria'])
    ->prefix('secretaria')
    ->name('secretaria.')
    ->group(function () {

        Route::get('/', [SecretariaController::class, 'index'])
            ->name('dashboard');

        // Secretaría: emitir constancias, certificados y verificar no adeudo
        Route::get('constancias', function () { return view('secretaria.constancias'); })->name('constancias');
        Route::get('certificados', function () { return view('secretaria.certificados'); })->name('certificados');
        Route::get('no-adeudo', function () { return view('secretaria.no_adeudo'); })->name('no_adeudo');
    });

/*
|--------------------------------------------------------------------------
| ESTUDIANTE
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'role:estudiante'])
    ->prefix('estudiante')
    ->name('estudiante.')
    ->group(function () {

        Route::get('/', [EstudianteController::class, 'index'])
            ->name('dashboard');

        // Estudiante: mis deudas, historial de pagos y trámites
        Route::get('deudas', function () {
            $deudas = []; // placeholder - implementar fuente real
            return view('estudiante.deudas', compact('deudas'));
        })->name('deudas');

        Route::get('pagos', function () {
            $pagos = \App\Models\Pago::where('email', auth()->user()->email)->get();
            return view('estudiante.pagos', compact('pagos'));
        })->name('pagos');

        Route::get('tramites', function () {
            return view('estudiante.tramites');
        })->name('tramites');
    });

require __DIR__.'/auth.php';