<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ReniecController;
use App\Http\Controllers\Admin\TipoUsuarioController;
use App\Http\Controllers\Codigo\CodigoIngresoController;
use App\Http\Controllers\Codigo\CodigoIngresoDetalleController;
use App\Http\Controllers\Codigo\CodigoEgresoController;
use App\Http\Controllers\Codigo\CodigoEgresoDetalleController;
use App\Http\Controllers\Cuentas\CuentaController;
use App\Http\Controllers\Proyecto\ProyectoController;
use App\Http\Controllers\Parametro\ComprobanteController;
use App\Http\Controllers\Caja\AperturaCierreController;
use App\Http\Controllers\Caja\CierreController;
use App\Http\Controllers\Caja\IngresoController;
use App\Http\Controllers\Caja\EgresoController;
use App\Http\Controllers\Caja\IngresoAnularController;
use App\Http\Controllers\Caja\SalidaAnularController;
use App\Http\Controllers\Caja\ReporteMovimientoController;
use App\Http\Controllers\Banco\MigrarDatosController;
use App\Http\Controllers\Banco\ReporteMigracioanController;


use App\Http\Controllers\Admin\UsuarioController;
use App\Http\Controllers\Admin\PrivilegioController;
use App\Http\Controllers\Admin\MenuController;
use App\Http\Controllers\Admin\SedeController;
use App\Http\Controllers\Admin\AuditoriaController;

Route::get('/', function () {
    return view('welcome');
})->name('home');

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

/*
|--------------------------------------------------------------------------
| Consulta RENIEC
|--------------------------------------------------------------------------
*/
Route::middleware(['auth'])->group(function () {
    Route::post('/reniec/consultar-dni', [ReniecController::class, 'consultarDni'])
        ->name('reniec.consultar.dni');
});

/*
|--------------------------------------------------------------------------
| Zona Admin
|--------------------------------------------------------------------------
*/
Route::middleware(['auth', 'menu.access'])->prefix('admin')->name('admin.')->group(function () {

    Route::get('/', function () {
        return redirect()->route('dashboard');
    })->name('dashboard');
    //select dinamicos
    Route::get('ajax/codigo-ingreso/{id_sede}', [CodigoIngresoController::class, 'ajaxCodigoIngreso'])
    ->name('ajax.codigo.ingreso');

    Route::get('ajax/codigo-ingreso-detalle/{id_codigo_ingreso}/{id_sede}', [CodigoIngresoDetalleController::class, 'ajaxCodigoIngresoDetalle'])
    ->name('ajax.codigo.ingreso.detalle');
    Route::get('ajax/codigo-egreso/{id_sede}', [CodigoEgresoController::class, 'ajaxCodigoEgreso'])
    ->name('ajax.codigo.egreso');
    Route::get('ajax/codigo-egreso-detalle/{id_codigo_egreso}/{id_sede}', [CodigoEgresoDetalleController::class, 'ajaxCodigoEgresoDetalle'])
    ->name('ajax.codigo.egreso.detalle');

    Route::get('ajax/ingreso-conceptos/{id_sede}', [IngresoController::class, 'ajaxConceptos'])
    ->name('ajax.ingreso.conceptos');

    Route::get('ajax/ingreso-detalles/{id_sede}', [IngresoController::class, 'ajaxDetalles'])
    ->name('ajax.ingreso.detalles');
    Route::get('ajax/egreso-conceptos/{id_sede}', [EgresoController::class, 'ajaxConceptos'])
    ->name('ajax.egreso.conceptos');
    Route::get('ajax/egreso-detalles/{id_sede}', [EgresoController::class, 'ajaxDetalles'])
    ->name('ajax.egreso.detalles');

    Route::resource('codigo-ingreso', CodigoIngresoController::class);
    Route::resource('codigo-ingreso-detalle', CodigoIngresoDetalleController::class);

    Route::resource('codigo-egreso', CodigoEgresoController::class);
    Route::resource('codigo-egreso-detalle', CodigoEgresoDetalleController::class);

    Route::resource('comprobante', ComprobanteController::class);
    Route::resource('proyecto', ProyectoController::class);

    Route::resource('cuenta', CuentaController::class)
        ->parameters(['cuenta' => 'cuenta']);

    Route::get('apertura-cierre/{id}/pdf', [AperturaCierreController::class, 'pdf'])->name('apertura-cierre.pdf');
    Route::resource('apertura-cierre', AperturaCierreController::class);
    Route::get('cierre/{id}/pdf', [CierreController::class, 'pdf'])->name('cierre.pdf');
    Route::resource('cierre', CierreController::class);
    Route::get('ingreso/{id}/pdf', [IngresoController::class, 'pdf'])->name('ingreso.pdf');
    Route::resource('ingreso', IngresoController::class);
    Route::get('egreso/{id}/pdf', [EgresoController::class, 'pdf'])->name('egreso.pdf');
    Route::resource('egreso', EgresoController::class);
    Route::get('reporte-movimientos', [ReporteMovimientoController::class, 'index'])->name('reporte-movimientos.index');
    Route::resource('ingreso-anular', IngresoAnularController::class);
    Route::resource('salida-anular', SalidaAnularController::class);

    // migrar datos
    Route::post('migrardatos/preview', [MigrarDatosController::class, 'preview'])->name('migrardatos.preview');
    Route::resource('migrardatos', MigrarDatosController::class)->only(['index', 'create', 'store']);

    Route::get('reportedemigracion/download/{token}', [ReporteMigracioanController::class, 'download'])->name('reportedemigracion.download');
    Route::resource('reportedemigracion', ReporteMigracioanController::class);


    Route::middleware('superadmin')->group(function () {
        Route::resource('usuario', UsuarioController::class);
        Route::resource('tipo-usuario', TipoUsuarioController::class);
        Route::resource('menu', MenuController::class);
        Route::resource('sede', SedeController::class);

        Route::get('auditoria', [AuditoriaController::class, 'index'])->name('auditoria.index');
        Route::get('auditoria/agrupado', [AuditoriaController::class, 'grouped'])->name('auditoria.grouped');
        Route::get('auditoria/export/csv', [AuditoriaController::class, 'exportCsv'])->name('auditoria.export.csv');
        Route::get('auditoria/export/pdf', [AuditoriaController::class, 'exportPdf'])->name('auditoria.export.pdf');

        Route::get('privilegios', [PrivilegioController::class, 'index'])->name('privilegios.index');
        Route::get('privilegios/{tipo_usuario}/edit', [PrivilegioController::class, 'edit'])->name('privilegios.edit');
        Route::put('privilegios/{tipo_usuario}', [PrivilegioController::class, 'update'])->name('privilegios.update');
    });
});

Route::middleware(['auth', 'menu.access'])->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');
});

require __DIR__.'/auth.php';
