<?php

use App\Http\Controllers\AreaController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\CatalogoSimpleController;
use App\Http\Controllers\ComponenteController;
use App\Http\Controllers\ConfiguracionController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\EquipoController;
use App\Http\Controllers\ImportacionController;
use App\Http\Controllers\MovilController;
use App\Http\Controllers\OrdenTrabajoController;
use App\Http\Controllers\PerfilController;
use App\Http\Controllers\PlanController;
use App\Http\Controllers\ProgramacionController;
use App\Http\Controllers\ReporteController;
use App\Http\Controllers\RepuestoController;
use App\Http\Controllers\SistemaController;
use App\Http\Controllers\UsuarioController;
use Illuminate\Support\Facades\Route;

// Auth
Route::get('/login', [LoginController::class, 'showLogin'])->name('login');
Route::post('/login', [LoginController::class, 'login']);
Route::post('/logout', [LoginController::class, 'logout'])->middleware('auth')->name('logout');

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

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

    // Perfil
    Route::get('/perfil', [PerfilController::class, 'edit'])->name('perfil.edit');
    Route::put('/perfil', [PerfilController::class, 'update'])->name('perfil.update');

    // Usuarios
    Route::resource('usuarios', UsuarioController::class)->parameters(['usuarios' => 'usuario']);

    // Configuración
    Route::get('/configuracion', [ConfiguracionController::class, 'index'])->name('configuracion.index');
    Route::post('/configuracion', [ConfiguracionController::class, 'update'])->name('configuracion.update');

    // Catálogos avanzados
    Route::resource('areas', AreaController::class)->except('show');
    Route::resource('sistemas', SistemaController::class)->except('show');
    Route::resource('equipos', EquipoController::class)->except('show');
    Route::resource('componentes', ComponenteController::class)->except('show');

    // Catálogos simples genéricos
    Route::get('/catalogos/{slug}', [CatalogoSimpleController::class, 'index'])->name('catalogos.index');
    Route::get('/catalogos/{slug}/nuevo', [CatalogoSimpleController::class, 'create'])->name('catalogos.create');
    Route::post('/catalogos/{slug}', [CatalogoSimpleController::class, 'store'])->name('catalogos.store');
    Route::get('/catalogos/{slug}/{id}/editar', [CatalogoSimpleController::class, 'edit'])->name('catalogos.edit');
    Route::put('/catalogos/{slug}/{id}', [CatalogoSimpleController::class, 'update'])->name('catalogos.update');
    Route::delete('/catalogos/{slug}/{id}', [CatalogoSimpleController::class, 'destroy'])->name('catalogos.destroy');

    // Aliases del menú para catálogos simples
    foreach (['tipos-mantenimiento','frecuencias','unidades','estados-orden','prioridades','actividades-lila'] as $slug) {
        Route::get("/$slug", fn () => redirect()->route('catalogos.index', $slug))->name("$slug.index");
    }

    // Planes
    Route::resource('planes', PlanController::class)->parameters(['planes' => 'plan']);
    Route::get('/planes/{plan}/pdf', [PlanController::class, 'pdf'])->name('planes.pdf');
    Route::post('/planes/{plan}/items', [PlanController::class, 'itemStore'])->name('planes.items.store');
    Route::put('/planes/{plan}/items/{item}', [PlanController::class, 'itemUpdate'])->name('planes.items.update');
    Route::delete('/planes/{plan}/items/{item}', [PlanController::class, 'itemDestroy'])->name('planes.items.destroy');

    // Programación
    Route::get('/programacion', [ProgramacionController::class, 'index'])->name('programacion.index');
    Route::post('/programacion/generar', [ProgramacionController::class, 'generar'])->name('programacion.generar');
    Route::post('/programacion/replanificar', [ProgramacionController::class, 'replanificar'])->name('programacion.replanificar');

    // Horómetros
    Route::get('/horometros', [\App\Http\Controllers\HorometroController::class, 'index'])->name('horometro.index');
    Route::post('/horometros', [\App\Http\Controllers\HorometroController::class, 'guardar'])->name('horometro.guardar');
    Route::get('/horometros/{equipo}/historial', [\App\Http\Controllers\HorometroController::class, 'historial'])->name('horometro.historial');

    // Paros de planta
    Route::resource('paros', \App\Http\Controllers\ParoPlantaController::class)->except('show');
    Route::post('/paros/{paro}/cerrar', [\App\Http\Controllers\ParoPlantaController::class, 'cerrar'])->name('paros.cerrar');

    // Órdenes
    Route::post('/ordenes/eliminar-multiples', [OrdenTrabajoController::class, 'eliminarMultiples'])->name('ordenes.eliminar-multiples');
    Route::resource('ordenes', OrdenTrabajoController::class)->parameters(['ordenes' => 'orden']);
    Route::get('/ordenes/{orden}/pdf', [OrdenTrabajoController::class, 'pdf'])->name('ordenes.pdf');
    Route::get('/ordenes/{orden}/informe.pdf', [\App\Http\Controllers\InformeController::class, 'pdf'])->name('ordenes.informe.pdf');
    Route::get('/ordenes/{orden}/informe.docx', [\App\Http\Controllers\InformeController::class, 'word'])->name('ordenes.informe.word');
    Route::post('/ordenes/{orden}/estado', [OrdenTrabajoController::class, 'cambiarEstado'])->name('ordenes.estado');
    Route::post('/ordenes/{orden}/firmar', [OrdenTrabajoController::class, 'firmar'])->name('ordenes.firmar');
    Route::post('/ordenes/{orden}/items', [OrdenTrabajoController::class, 'itemStore'])->name('ordenes.items.store');
    Route::put('/ordenes/{orden}/items/{item}', [OrdenTrabajoController::class, 'actualizarItem'])->name('ordenes.items.update');
    Route::delete('/ordenes/{orden}/items/{item}', [OrdenTrabajoController::class, 'itemDestroy'])->name('ordenes.items.destroy');
    Route::post('/ordenes/{orden}/poblar-plan', [OrdenTrabajoController::class, 'poblarDesdePlan'])->name('ordenes.poblar');
    Route::post('/ordenes/{orden}/fotos', [OrdenTrabajoController::class, 'subirFoto'])->name('ordenes.fotos.store');
    Route::delete('/ordenes/{orden}/fotos/{foto}', [OrdenTrabajoController::class, 'eliminarFoto'])->name('ordenes.fotos.destroy');
    Route::post('/ordenes/{orden}/costos', [OrdenTrabajoController::class, 'agregarCosto'])->name('ordenes.costos.store');
    Route::delete('/ordenes/{orden}/costos/{costo}', [OrdenTrabajoController::class, 'eliminarCosto'])->name('ordenes.costos.destroy');

    // Móvil
    Route::get('/movil', [MovilController::class, 'index'])->name('movil.index');

    // Importación
    Route::get('/importacion', [ImportacionController::class, 'index'])->name('importacion.index');
    Route::post('/importacion', [ImportacionController::class, 'store'])->name('importacion.store');
    Route::get('/importacion/{importacion}', [ImportacionController::class, 'show'])->name('importacion.show');

    // Repuestos
    Route::resource('repuestos', RepuestoController::class)->except('show');
    Route::post('/repuestos/{repuesto}/entrada', [RepuestoController::class, 'entrada'])->name('repuestos.entrada');

    // Reportes
    Route::get('/reportes', [ReporteController::class, 'index'])->name('reportes.index');
    Route::get('/reportes/ordenes.xlsx', [ReporteController::class, 'exportarOrdenesExcel'])->name('reportes.ordenes.excel');
    Route::get('/reportes/ordenes.pdf', [ReporteController::class, 'exportarOrdenesPdf'])->name('reportes.ordenes.pdf');
    Route::get('/reportes/cumplimiento', [ReporteController::class, 'cumplimiento'])->name('reportes.cumplimiento');
});
