<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FormularioController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\EstadisticasController;

/* Home al entrar a /Auditoria-app/public/ */
Route::get('/', [HomeController::class, 'index'])->name('root');

/* Todo el módulo bajo /auditoria_capita */
Route::prefix('auditoria_capita')->group(function () {
    /* Home del módulo */
    Route::get('/', [HomeController::class, 'index'])->name('home');

    /* Estadísticas */
    Route::get('/estadisticas', [EstadisticasController::class, 'index'])
        ->name('estadisticas.index');

    /* Formularios (UI/HTML/PDF) */
    Route::get('/formularios/htmls', [HomeController::class, 'htmlSnapshots'])
        ->name('formularios.htmls');

    Route::post('/formularios/pdf', [FormularioController::class, 'pdf'])
        ->name('formularios.pdf');

    Route::get('/formularios/crear', [FormularioController::class, 'create'])
        ->name('formularios.create');

    Route::post('/formularios', [FormularioController::class, 'store'])
        ->name('formularios.store');

    Route::post('/formularios/estadisticas/guardar', [FormularioController::class, 'guardarEstadisticas'])
        ->name('formularios.estadisticas.guardar');
    
    
    Route::post('/formularios/{formulario}/finalizar', [FormularioController::class, 'finalizarRevision'])
        ->name('formularios.finalizar');

    Route::get('/formularios/{formulario}/revision', [FormularioController::class, 'show'])
        ->name('formularios.revision');


    /* APIs (selects / typeahead) */
    Route::prefix('api')->group(function () {
        Route::get('/municipios', [HomeController::class, 'municipios'])
            ->name('api.municipios');

        Route::get('/nits', [HomeController::class, 'nits'])
            ->name('api.nits'); // typeahead

        Route::get('/sedes-por-nit', [HomeController::class, 'sedesPorNit'])
            ->name('api.sedes.porNit');
    });

    /* Rutas públicas auxiliares (no API) */
    Route::get('/nits', [HomeController::class, 'nits'])
        ->name('nits');

    /* Acciones */
    Route::post('/buscar-sede', [HomeController::class, 'buscarSede'])
        ->name('buscar.sede');

    /* Catch-all del módulo: cualquier /auditoria_capita/* no mapeado -> home */
    Route::get('/{any}', function () {
        return redirect()->route('home');
    })->where('any', '.*');
});
