<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\AcademiaController;
use App\Http\Controllers\VinculacionController;
use App\Http\Controllers\Administracion\administradorController;
use App\Http\Controllers\Administracion\AreaController;
use App\Http\Controllers\Administracion\CursoController;
use App\Http\Controllers\Administracion\PreguntaController;
use App\Http\Controllers\Administracion\ProcesoController;
use App\Http\Controllers\PostulanteAuthController;
use App\Http\Controllers\ExamenController;
use App\Http\Controllers\Administracion\ReglaAreaProcesoController;
use App\Http\Controllers\Administracion\ProcesoAdmisionController;
use App\Http\Controllers\Administracion\ProcesoAdmisionDetalleController;
use App\Http\Controllers\Administracion\PostulanteController;
use App\Http\Controllers\Administracion\CalificacionController;
use App\Http\Controllers\Administracion\NoticiaController;
use App\Http\Controllers\WebController;

Route::get('/user', function (Request $request) {
     return $request->user();
 })->middleware('auth:sanctum');

Route::post('/register', [AuthController::class, 'register']);
Route::post('/login', [AuthController::class, 'login']);

Route::middleware('auth:sanctum')->group(function () {
    Route::post('/logout', [AuthController::class, 'logout']);
    Route::get('/me', [AuthController::class, 'me']);
    Route::post('/refresh-token', [AuthController::class, 'refresh']);
});


Route::middleware(['auth:sanctum'])->prefix('procesos')->group(function () {
    Route::get('/', [ProcesoController::class, 'index']);
    Route::post('/', [ProcesoController::class, 'store']);
    Route::get('{id}', [ProcesoController::class, 'show']);
    Route::put('{id}', [ProcesoController::class, 'update']);
    Route::delete('{id}', [ProcesoController::class, 'destroy']);
    Route::patch('{id}/toggle-activo', [ProcesoController::class, 'toggleActivo']);
});


Route::middleware(['auth:sanctum'])->prefix('admin')->group(function () {

    Route::get('/areas', [AreaController::class, 'index']); 
    Route::post('/areas', [AreaController::class, 'store']);    
    Route::get('/areas/{id}', [AreaController::class, 'show']);  
    Route::put('/areas/{id}', [AreaController::class, 'update']); 
    Route::delete('/areas/{id}', [AreaController::class, 'destroy']); 
    Route::patch('/areas/{id}/toggle', [AreaController::class, 'toggleEstado']);

    Route::post('/areas/{area}/vincular-cursos', [AreaController::class, 'vincularCursosArea']);
    Route::post('/areas/{area}/desvincular-curso', [AreaController::class, 'desvincularCursoArea']);
    Route::get('/areas/{area}/cursos-disponibles', [AreaController::class, 'getCursosPorArea']);


    Route::post('areas/{area}/vincular-procesos', [AreaController::class, 'vincularProcesosArea']);
    Route::get('areas/{area}/procesos-disponibles', [AreaController::class, 'getProcesosPorArea'] );
    Route::post('areas/{area}/desvincular-procesos', [AreaController::class, 'desvincularProcesoArea'] );


});

Route::middleware(['auth:sanctum'])->prefix('admin')->group(function () {

    // NOTICIAS
    Route::get('/noticias', [NoticiaController::class, 'index']);
    Route::get('/noticias/{noticia}', [NoticiaController::class, 'show']);

    Route::post('/noticias', [NoticiaController::class, 'store']);

    // usa SOLO UNA (PUT o PATCH). Aquí dejo PUT:
    Route::put('/noticias/{noticia}', [NoticiaController::class, 'update']);

    Route::delete('/noticias/{noticia}', [NoticiaController::class, 'destroy']);
});
Route::get('/noticias', [NoticiaController::class, 'index']);
Route::get('/noticias/{noticia:slug}', [NoticiaController::class, 'showPublic']);

Route::middleware(['auth:sanctum'])->prefix('admin')->group(function () {

    Route::get('/cursos', [CursoController::class, 'index']);
    Route::post('/cursos', [CursoController::class, 'store']);
    Route::get('/cursos/{id}', [CursoController::class, 'show']);
    Route::put('/cursos/{id}', [CursoController::class, 'update']);
    Route::delete('/cursos/{id}', [CursoController::class, 'destroy']);
    Route::patch('/cursos/{id}/toggle', [CursoController::class, 'toggleEstado']);

});

Route::middleware(['auth:sanctum'])->prefix('admin')->group(function () {

    Route::get('/postulantes', [PostulanteController::class, 'obtenerPostulantes']);
    Route::put('/postulantes/{id}', [PostulanteController::class, 'actualizarPostulante']);
});




Route::middleware(['auth:sanctum'])->prefix('admin')->group(function () {

    Route::get('cursos/{cursoId}/preguntas', [PreguntaController::class, 'getPreguntasCurso']);
    Route::post('preguntas', [PreguntaController::class, 'agregarPreguntaCurso']);
    Route::get('preguntas/{id}', [PreguntaController::class, 'getPregunta'] );
    Route::put('preguntas/{id}', [PreguntaController::class, 'actualizarPregunta']);
    Route::delete('preguntas/{id}', [PreguntaController::class, 'eliminarPregunta'] );

});

Route::middleware('auth:sanctum')->group(function () {


    Route::get('/calificaciones', [CalificacionController::class, 'index']);
    Route::post('/calificaciones', [CalificacionController::class, 'store']);
    Route::get('/calificaciones/{id}', [CalificacionController::class, 'show']);
    Route::put('/calificaciones/{id}', [CalificacionController::class, 'update']);
    Route::delete('/calificaciones/{id}', [CalificacionController::class, 'destroy']);

});



Route::prefix('postulante')->group(function () {

    // Registro
    Route::post('/register', [PostulanteAuthController::class, 'register']);

    // Login
    Route::post('/login', [PostulanteAuthController::class, 'login']);

    // Rutas protegidas por token
    Route::middleware('auth:sanctum')->group(function () {
        Route::post('/logout', [PostulanteAuthController::class, 'logout']);
        Route::get('/me', [PostulanteAuthController::class, 'me']);
        Route::get('/pagos', [PostulanteAuthController::class, 'obtenerPagosPostulante']);
        Route::get('/postulante/mis-procesos',[PostulanteAuthController::class, 'misProcesos']);
        Route::get('/mis-procesos', [PostulanteAuthController::class, 'misProcesos']);

    });


});


// Route::middleware('auth:sanctum')->group(function () {
//     Route::get('/procesos', [ExamenController::class, 'procesoexamen']);
//     Route::get('/areas', [ExamenController::class, 'areas']);
//     Route::post('/examen', [ExamenController::class, 'store']);
//     Route::get('/examen/actual', [ExamenController::class, 'miExamenActual']);
// });


Route::middleware(['auth:sanctum'])->prefix('area-proceso')->group(function () {
    Route::get('areasprocesos', [ReglaAreaProcesoController::class, 'areasProcesos']);
    Route::prefix('{areaProcesoId}/reglas')->group(function () {
        Route::get('/', [ReglaAreaProcesoController::class, 'index']);           // Listar reglas
        Route::post('/', [ReglaAreaProcesoController::class, 'store']);          // Crear/actualizar regla individual
        Route::post('/multiple', [ReglaAreaProcesoController::class, 'storeMultiple']); // Guardar múltiples reglas
    });
});

Route::middleware(['auth:sanctum'])->prefix('reglas')->group(function () {
    Route::put('/{reglaId}', [ReglaAreaProcesoController::class, 'update']);      // Editar regla
    Route::delete('/{reglaId}', [ReglaAreaProcesoController::class, 'destroy']);  // Eliminar regla
});



// Examen - Flujo separado
Route::middleware(['auth:postulante'])->group(function () {

    Route::get('/examen/procesos', [ExamenController::class, 'procesoexamen']);
    Route::get('/examen/areas', [ExamenController::class, 'areas']);
    Route::get('/examen/actual', [ExamenController::class, 'miExamenActual']);
    
    // Crear examen (sin preguntas)
    Route::post('/examen/crear', [ExamenController::class, 'crearExamen']);
    
    // Generar preguntas
    Route::post('/examen/{examen}/generar-preguntas', [ExamenController::class, 'generarPreguntas']);
    
    // Obtener preguntas
    Route::get('/examen/{examen}/preguntas', [ExamenController::class, 'obtenerPreguntas']);
    
    // Iniciar examen (marcar hora inicio)
    Route::post('/examen/iniciar', [ExamenController::class, 'iniciarExamen']);
    
    // Responder preguntas
    Route::post('/examen/pregunta/{pregunta}/responder', [ExamenController::class, 'responderPregunta']);
    
    // Finalizar examen
    Route::post('/examen/{examen}/finalizar', [ExamenController::class, 'finalizarExamen']);
    Route::post('/examen/{examenId}/calificar', [ExamenController::class, 'calificarExamen']);
    
});


Route::middleware('auth:sanctum')->prefix('admin')->group(function () {

        // PROCESOS
        Route::prefix('procesos-admision')->group(function () {
            Route::get('/', [ProcesoAdmisionController::class, 'index'])->name('index');
            Route::post('/', [ProcesoAdmisionController::class, 'store'])->name('store');
            Route::get('/{id}', [ProcesoAdmisionController::class, 'show'])->name('show');
            Route::match(['put','patch'], '/{id}', [ProcesoAdmisionController::class, 'update'])->name('update');
            Route::delete('/{id}', [ProcesoAdmisionController::class, 'destroy'])->name('destroy');

            // DETALLES por proceso
            Route::get('/{procesoId}/detalles', [ProcesoAdmisionDetalleController::class, 'index'])->name('detalles.index');
            Route::post('/{procesoId}/detalles', [ProcesoAdmisionDetalleController::class, 'store'])->name('detalles.store');
        });

        // DETALLES por ID
        Route::prefix('detalles-admision')->group(function () {
            Route::get('/{id}', [ProcesoAdmisionDetalleController::class, 'show'])->name('show');
            Route::match(['put','patch'], '/{id}', [ProcesoAdmisionDetalleController::class, 'update'])->name('update');
            Route::delete('/{id}', [ProcesoAdmisionDetalleController::class, 'destroy'])->name('destroy');
        });
    });



Route::get('/procesos-admision', [WebController::class, 'GetProcesoAdmision']);
