<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MetricaController;
use App\Http\Controllers\SectorController;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\TipoViaController;
use App\Http\Controllers\TipoZonaController;
use App\Http\Controllers\UrbanizacionController;
use App\Http\Controllers\ViaController;
use App\Http\Controllers\LotizacionController;
use App\Http\Controllers\TerrenoController;
use App\Http\Controllers\MedidorController;
use App\Http\Controllers\ClienteController;

use App\Http\Controllers\ConexionController;
use App\Http\Controllers\LecturaController;
use App\Http\Controllers\ClaseTarifaController;
use App\Http\Controllers\CategoriaTarifaController;
use App\Http\Controllers\TarifaController;
use App\Http\Controllers\CronogramaFacturacionController;
use App\Http\Controllers\FacturacionController;
use App\Http\Controllers\ReciboController;
use App\Http\Controllers\PagoController;
use App\Http\Controllers\CajaController;
use App\Http\Controllers\ReporteController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\DeudaInicialConexionController;

Route::get('/', function () {
    return view('auth.login');
});

Auth::routes();

Route::middleware('auth')->group(function () {
    // Rutas de recursos primero
    Route::get('/sectores/listar', [SectorController::class, 'listar']);
    Route::get('/sectores/{id}/vias', [SectorController::class, 'viasPorSector']);
    Route::get('/sectores/{id}/urbanizaciones', [TerrenoController::class, 'urbanizaciones']);
    Route::resource('sectores', SectorController::class)->middleware('auth');

    Route::resource('tipovia', TipoViaController::class)->middleware('auth');

    Route::resource('tipozona', TipoZonaController::class)->middleware('auth');

    Route::resource('urbanizacion', UrbanizacionController::class)->middleware('auth');

    Route::resource('via', ViaController::class)->middleware('auth');
    Route::resource('lotizacion', LotizacionController::class)->middleware('auth');
    Route::resource('terrenos', TerrenoController::class)->middleware('auth');
    Route::resource('medidor', MedidorController::class)->middleware('auth');
    
    Route::resource('cliente', ClienteController::class)->middleware('auth');


Route::post('/conexion/importar-excel', [ConexionController::class, 'importarExcel'])
    ->name('conexion.importar.excel');


    Route::resource('conexiones', ConexionController::class)->middleware('auth');
    Route::resource('lecturas', LecturaController::class)->middleware('auth');
    Route::resource('clase-tarifa', ClaseTarifaController::class)->middleware('auth');
    Route::resource('categoria_tarifa', CategoriaTarifaController::class)->middleware('auth');
    Route::resource('tarifas', TarifaController::class)->middleware('auth');
    
    Route::get('/cronogramas/filtrar', [CronogramaFacturacionController::class, 'filtrar'])->name('cronogramas.filtrar');

    Route::get('/cronogramas/meses/{anio}', [CronogramaFacturacionController::class, 'mesesPorAnio']);


    Route::resource('cronogramas', CronogramaFacturacionController::class)->middleware('auth');

    Route::resource('facturacion', FacturacionController::class)
    ->except(['show']);

Route::get('/clientes/{id}/conexiones', [ClienteController::class, 'conexiones'])
    ->name('clientes.conexiones');
    Route::get('/clientes/buscar', [ClienteController::class, 'buscar'])->name('clientes.buscar');
    Route::get('/medidores/buscar', [MedidorController::class, 'buscar'])->name('medidores.buscar');


    // web.php o api.php
    // Rutas dinámicas para selects
    

    
    Route::get('/urbanizaciones/{id}/vias-lotizaciones', [TerrenoController::class, 'viasLotizaciones']);
    Route::get('/terrenos/{tipo}/{id}', [TerrenoController::class, 'terrenos']);

    // Rutas dinámicas buscar las conexiones y lecturas
    Route::get('/lecturas', [LecturaController::class, 'index'])->name('lecturas.index');
    Route::post('/lecturas/buscar', [LecturaController::class, 'buscarConexiones'])->name('lecturas.buscar');
    Route::post('/lecturas/guardar', [LecturaController::class, 'guardar'])->name('lecturas.guardar');

    Route::post('/lecturas/importar-excel',[LecturaController::class, 'importarExcel'])->name('lecturas.importarExcel');

    Route::post('/facturacion/generar', [FacturacionController::class, 'generarAjax'])->name('facturacion.generar');


     // Rutas dinámicas recibos
    Route::get('/recibos/imprimir/{id_cronograma}', [ReciboController::class, 'imprimir'])->name('recibos.imprimir');

    Route::post('/recibos/generar', [ReciboController::class, 'generarRecibos'])->name('recibos.generar');
    
    Route::get('/recibos/buscar', [ReciboController::class, 'buscarPorDNI'])->name('recibos.buscar');
    Route::post('/recibos/filtrar-dni', [ReciboController::class, 'filtrarPorDNI'])->name('recibos.filtrar-dni');
    Route::get('/recibos/imprimir/individual/{id}', [ReciboController::class, 'imprimirReciboIndividual'])->name('recibos.imprimir.individual');

    Route::get('/recibos/filtrar', [ReciboController::class, 'filtrar'])->name('recibos.filtrar');
    Route::resource('recibos', ReciboController::class)->middleware('auth');


Route::resource('pagos', PagoController::class)->middleware('auth');


// Buscar recibos por documento
Route::get('/buscar-recibos/{documento}', [PagoController::class, 'buscarRecibos']);

// Guardar pago
Route::post('/pagos/store', [PagoController::class, 'store'])->name('pagos.store');


// Ruta para imprimir el recibo
Route::get('/pagos/{id}/imprimir', [PagoController::class, 'imprimir'])->name('pagos.imprimir');
Route::get('/pagos/{id}/ticket', [PagoController::class, 'mostrarTicket'])->name('pagos.ticket');

Route::prefix('caja')->group(function () {
    Route::get('/', [CajaController::class, 'index'])->name('caja.index');
    Route::post('/abrir', [CajaController::class, 'abrir'])->name('caja.abrir');
    Route::post('/cerrar/{id}', [CajaController::class, 'cerrar'])->name('caja.cerrar');
    Route::get('/historial/{id}', [CajaController::class, 'historial'])->name('caja.historial');
});

Route::prefix('reportes')->name('reportes.')->group(function () {
    Route::get('/', [ReporteController::class, 'index'])->name('index');
    Route::get('/recibos-vencidos', [ReporteController::class, 'recibosVencidos'])->name('recibos_vencidos');
    Route::get('/pagos', [ReporteController::class, 'pagos'])->name('pagos');
    Route::get('/cierre-caja', [ReporteController::class, 'cierreCaja'])->name('cierre_caja');

    // Nuevos reportes de recaudación
    Route::get('/proyeccion-recaudacion', [ReporteController::class, 'proyeccionRecaudacion'])->name('proyeccion_recaudacion');
    Route::get('/recaudado-real', [ReporteController::class, 'recaudadoReal'])->name('recaudado_real');
    // Reporte Pagos por Cronograma


});

Route::get('reportes/pagos-cronograma', [ReporteController::class, 'pagosPorCronograma'])->name('reportes.pagos_cronograma');
// routes/web.php
Route::get('/reportes/pagos-cronograma/ajax/{id}', [ReporteController::class, 'pagosCronogramaAjax'])
    ->name('reportes.pagos_cronograma.ajax');
Route::get('/reportes/pagos-cronograma/exportar/{id_cronograma}', [ReporteController::class, 'exportarPagos']);
Route::get('/facturacion/filtrar', [FacturacionController::class, 'filtrarFacturas'])->name('facturacion.filtrar');


Route::resource('users', UserController::class);
Route::resource('roles', RoleController::class);


// Ruta AJAX para listar cronogramas por sector (o general si está vacío)
Route::get('/facturacion/cronogramas/{sectorId?}', [FacturacionController::class, 'getCronogramasPorSector'])
    ->name('facturacion.cronogramas');

Route::prefix('deuda-inicial')
    ->name('deudas_inicial.')
    ->group(function () {

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

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

        Route::put('/{id}', [DeudaInicialConexionController::class, 'update'])
            ->name('update');

        Route::post('/{id}/cancelar', [DeudaInicialConexionController::class, 'cancelar'])
            ->name('cancelar');

        Route::get('/conexion/{id}', [DeudaInicialConexionController::class, 'obtenerPorConexion'])
            ->name('por_conexion');

            
});
Route::post('/deuda/importar-excel', [DeudaInicialConexionController::class, 'importarExcel'])->name('deuda.importarExcel');


Route::get('/dashboard/data', [MetricaController::class, 'dashboardData'])->name('dashboard.data');
    // Ruta catch-all al final
    Route::get('/{any}', [MetricaController::class, 'index'])->where('any', '.*')->name('dashboard');

});
