<?php

use App\Http\Controllers\Auth\TenantSelectionController;
use App\Http\Controllers\CajaController;
use App\Http\Controllers\CategoriaController;
use App\Http\Controllers\ClienteController;
use App\Http\Controllers\CompraController;
use App\Http\Controllers\ConfiguracionController;
use App\Http\Controllers\ManyChatIntegracionController;
use App\Http\Controllers\WompiIntegracionController;
use App\Http\Controllers\CuentasCobrarController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\EmpresaController;
use App\Http\Controllers\InventarioController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegistroEmpresaController;
use App\Http\Controllers\MediaController;
use App\Http\Controllers\NotificacionController;
use App\Http\Controllers\OrdenCompraController;
use App\Http\Controllers\PlanController;
use App\Http\Controllers\PosController;
use App\Http\Controllers\ProductoController;
use App\Http\Controllers\ProveedorController;
use App\Http\Controllers\PerfilController;
use App\Http\Controllers\ReporteController;
use App\Http\Controllers\SoporteController;
use App\Http\Controllers\SucursalController;
use App\Http\Controllers\AyudaIaController;
use App\Http\Controllers\UsuarioController;
use App\Http\Controllers\EnvioController;
use App\Http\Controllers\VentaController;
use Illuminate\Support\Facades\Route;

Route::get('/', static function () {
    return redirect()->route('dashboard');
});

Route::get('/media/{path}', [MediaController::class, 'show'])
    ->where('path', '.*')
    ->name('media.show');

Route::middleware('guest')->group(function (): void {
    Route::get('/login', [LoginController::class, 'create'])->name('login');
    Route::post('/login', [LoginController::class, 'store'])->name('login.store');

    Route::get('/registro-empresa', [RegistroEmpresaController::class, 'create'])->name('registro-empresa.create');
    Route::post('/registro-empresa', [RegistroEmpresaController::class, 'store'])->name('registro-empresa.store');
});

Route::middleware('auth')->group(function (): void {
    Route::post('/logout', [LoginController::class, 'destroy'])->name('logout');
    Route::get('/tenant/select', [TenantSelectionController::class, 'create'])->name('tenant.select');
    Route::post('/tenant/select', [TenantSelectionController::class, 'store'])->name('tenant.select.store');
    Route::get('/notificaciones', [NotificacionController::class, 'index'])->name('notificaciones.index');
    Route::post('/notificaciones/marcar-leidas', [NotificacionController::class, 'markRead'])->name('notificaciones.mark-read');
    Route::get('/perfil', [PerfilController::class, 'index'])->name('perfil.index');
    Route::post('/perfil', [PerfilController::class, 'update'])->name('perfil.update');
    Route::post('/perfil/password', [PerfilController::class, 'updatePassword'])->name('perfil.password.update');
    Route::get('/superadmin', [DashboardController::class, 'superadmin'])
        ->middleware('permission:superadmin.acceso')
        ->name('superadmin.index');
    Route::post('/superadmin/suscripciones/{suscripcion}/actualizar', [DashboardController::class, 'updateSubscription'])
        ->middleware('permission:superadmin.acceso')
        ->name('superadmin.suscripciones.update');
    Route::post('/superadmin/empresas/{empresa}/eliminar', [EmpresaController::class, 'destroy'])
        ->middleware('permission:superadmin.acceso')
        ->name('superadmin.empresas.destroy');

    // Superadmin — gestión de planes
    Route::middleware('permission:superadmin.acceso')->group(function () {
        Route::get('/superadmin/planes', [PlanController::class, 'index'])->name('superadmin.planes.index');
        Route::get('/superadmin/planes/crear', [PlanController::class, 'create'])->name('superadmin.planes.create');
        Route::post('/superadmin/planes', [PlanController::class, 'store'])->name('superadmin.planes.store');
        Route::get('/superadmin/planes/{plan}/editar', [PlanController::class, 'edit'])->name('superadmin.planes.edit');
        Route::put('/superadmin/planes/{plan}', [PlanController::class, 'update'])->name('superadmin.planes.update');
        Route::delete('/superadmin/planes/{plan}', [PlanController::class, 'destroy'])->name('superadmin.planes.destroy');
        Route::get('/superadmin/empresas/{empresaId}/modulos', [PlanController::class, 'empresaModulos'])->name('superadmin.empresas.modulos');
        Route::post('/superadmin/empresas/{empresaId}/modulos', [PlanController::class, 'empresaModulosUpdate'])->name('superadmin.empresas.modulos.update');
    });
});

Route::middleware(['auth', 'tenant'])->group(function (): void {
    Route::get('/dashboard', [DashboardController::class, 'index'])
        ->middleware('permission:dashboard.ver')
        ->name('dashboard');

    Route::get('/empresas', [EmpresaController::class, 'index'])->middleware('permission:empresas.ver')->name('empresas.index');
    Route::post('/empresas', [EmpresaController::class, 'store'])->middleware('permission:empresas.crear')->name('empresas.store');
    Route::get('/empresas/{empresa}/editar', [EmpresaController::class, 'edit'])->middleware('permission:empresas.ver')->name('empresas.edit');
    Route::put('/empresas/{empresa}', [EmpresaController::class, 'update'])->middleware('permission:empresas.crear')->name('empresas.update');
    Route::patch('/empresas/{empresa}/desactivar', [EmpresaController::class, 'deactivate'])->middleware('permission:empresas.desactivar')->name('empresas.deactivate');
    Route::patch('/empresas/{empresa}/reactivar', [EmpresaController::class, 'reactivate'])->middleware('permission:empresas.reactivar')->name('empresas.reactivate');
    Route::delete('/empresas/{empresa}', [EmpresaController::class, 'destroy'])->middleware('permission:empresas.eliminar')->name('empresas.destroy');

    Route::get('/sucursales', [SucursalController::class, 'index'])->middleware('permission:sucursales.ver')->name('sucursales.index');
    Route::post('/sucursales', [SucursalController::class, 'store'])->middleware('permission:sucursales.crear')->name('sucursales.store');
    Route::get('/sucursales/{sucursal}/editar', [SucursalController::class, 'edit'])->middleware('permission:sucursales.ver')->name('sucursales.edit');
    Route::put('/sucursales/{sucursal}', [SucursalController::class, 'update'])->middleware('permission:sucursales.editar')->name('sucursales.update');
    Route::post('/sucursales/{sucursal}/actualizar', [SucursalController::class, 'updateInline'])->middleware('permission:sucursales.editar')->name('sucursales.update-inline');
    Route::post('/sucursales/{sucursal}/estado', [SucursalController::class, 'toggle'])->middleware('permission:sucursales.editar')->name('sucursales.toggle');
    Route::post('/sucursales/{sucursal}/usuarios', [SucursalController::class, 'assignUser'])->middleware('permission:sucursales.editar')->name('sucursales.users.assign');
    Route::post('/sucursales/{sucursal}/usuarios/{usuario}/quitar', [SucursalController::class, 'unassignUser'])->middleware('permission:sucursales.editar')->name('sucursales.users.unassign');
    Route::post('/sucursales/{sucursal}/cajas', [SucursalController::class, 'storeCaja'])->middleware('permission:sucursales.editar')->name('sucursales.cash-boxes.store');
    Route::post('/sucursales/cajas/{caja}/estado', [SucursalController::class, 'toggleCaja'])->middleware('permission:sucursales.editar')->name('sucursales.cash-boxes.toggle');
    Route::patch('/sucursales/{sucursal}/desactivar', [SucursalController::class, 'deactivate'])->middleware('permission:sucursales.desactivar')->name('sucursales.deactivate');
    Route::patch('/sucursales/{sucursal}/reactivar', [SucursalController::class, 'reactivate'])->middleware('permission:sucursales.reactivar')->name('sucursales.reactivate');
    Route::delete('/sucursales/{sucursal}', [SucursalController::class, 'destroy'])->middleware('permission:sucursales.eliminar')->name('sucursales.destroy');

    Route::get('/usuarios', [UsuarioController::class, 'index'])->middleware('permission:usuarios.ver')->name('usuarios.index');
    Route::post('/usuarios', [UsuarioController::class, 'store'])->middleware('permission:usuarios.crear')->name('usuarios.store');
    Route::post('/usuarios/roles/permisos', [UsuarioController::class, 'updateRolePermissions'])->middleware('permission:roles.gestionar')->name('usuarios.roles.permissions.update');
    Route::get('/usuarios/{usuario}/editar', [UsuarioController::class, 'edit'])->middleware('permission:usuarios.ver')->name('usuarios.edit');
    Route::put('/usuarios/{usuario}', [UsuarioController::class, 'update'])->middleware('permission:roles.gestionar')->name('usuarios.update');
    Route::patch('/usuarios/{usuario}/desactivar', [UsuarioController::class, 'deactivate'])->middleware('permission:usuarios.desactivar')->name('usuarios.deactivate');
    Route::patch('/usuarios/{usuario}/reactivar', [UsuarioController::class, 'reactivate'])->middleware('permission:usuarios.reactivar')->name('usuarios.reactivate');
    Route::delete('/usuarios/{usuario}', [UsuarioController::class, 'destroy'])->middleware('permission:usuarios.eliminar')->name('usuarios.destroy');

    Route::get('/configuracion', [ConfiguracionController::class, 'index'])->middleware('permission:configuracion.ver')->name('configuracion.index');
    Route::post('/configuracion/empresa', [ConfiguracionController::class, 'updateCompany'])->middleware('permission:configuracion.editar')->name('configuracion.company.update');
    Route::post('/configuracion/ticket-size', [ConfiguracionController::class, 'updateTicketSize'])->middleware('permission:configuracion.editar')->name('configuracion.ticket-size.update');
    Route::post('/configuracion/fiscal', [ConfiguracionController::class, 'updateFiscal'])->middleware('permission:configuracion.editar')->name('configuracion.fiscal.update');
    Route::post('/configuracion/fiscal/facturacion-electronica', [ConfiguracionController::class, 'updateElectronicBilling'])->middleware('permission:configuracion.editar')->name('configuracion.electronic-billing.update');
    Route::post('/configuracion/fiscal/factus/probar', [ConfiguracionController::class, 'testFactusConnection'])->middleware('permission:configuracion.editar')->name('configuracion.electronic-billing.test-connection');
    Route::post('/configuracion/fiscal/factus/probar-factura', [ConfiguracionController::class, 'testFactusInvoice'])->middleware('permission:configuracion.editar')->name('configuracion.electronic-billing.test-invoice');
    Route::post('/configuracion/fiscal/factus/rangos', [ConfiguracionController::class, 'loadFactusRanges'])->middleware('permission:configuracion.editar')->name('configuracion.electronic-billing.load-ranges');
    Route::post('/configuracion/fiscal/factus/municipios', [ConfiguracionController::class, 'loadFactusMunicipalities'])->middleware('permission:configuracion.editar')->name('configuracion.electronic-billing.load-municipalities');
    Route::post('/configuracion/impuestos', [ConfiguracionController::class, 'storeTax'])->middleware('permission:configuracion.editar')->name('configuracion.taxes.store');
    Route::post('/configuracion/impuestos/{impuesto}/estado', [ConfiguracionController::class, 'toggleTax'])->middleware('permission:configuracion.editar')->name('configuracion.taxes.toggle');
    Route::post('/configuracion/metodos-pago', [ConfiguracionController::class, 'storePaymentMethod'])->middleware('permission:configuracion.editar')->name('configuracion.payment-methods.store');
    Route::post('/configuracion/metodos-pago/{metodoPago}/estado', [ConfiguracionController::class, 'togglePaymentMethod'])->middleware('permission:configuracion.editar')->name('configuracion.payment-methods.toggle');
    Route::post('/configuracion/unidades-medida', [ConfiguracionController::class, 'storeMeasurementUnit'])->middleware('permission:configuracion.editar')->name('configuracion.measurement-units.store');
    Route::post('/configuracion/unidades-medida/{unidadMedida}/actualizar', [ConfiguracionController::class, 'updateMeasurementUnit'])->middleware('permission:configuracion.editar')->name('configuracion.measurement-units.update');
    Route::post('/configuracion/unidades-medida/{unidadMedida}/estado', [ConfiguracionController::class, 'toggleMeasurementUnit'])->middleware('permission:configuracion.editar')->name('configuracion.measurement-units.toggle');
    Route::post('/configuracion/unidades-medida/{unidadMedida}/eliminar', [ConfiguracionController::class, 'deleteMeasurementUnit'])->middleware('permission:configuracion.editar')->name('configuracion.measurement-units.destroy');
    Route::post('/configuracion/numeraciones', [ConfiguracionController::class, 'storeDocumentNumbering'])->middleware('permission:configuracion.editar')->name('configuracion.numberings.store');
    Route::post('/configuracion/numeraciones/{numeracion}/estado', [ConfiguracionController::class, 'toggleDocumentNumbering'])->middleware('permission:configuracion.editar')->name('configuracion.numberings.toggle');
    Route::post('/configuracion/resoluciones', [ConfiguracionController::class, 'storeDianResolution'])->middleware('permission:configuracion.editar')->name('configuracion.resolutions.store');
    Route::post('/configuracion/resoluciones/{resolucion}/estado', [ConfiguracionController::class, 'toggleDianResolution'])->middleware('permission:configuracion.editar')->name('configuracion.resolutions.toggle');

    // Veltrix WhatsApp IA — ManyChat panel
    Route::get('/configuracion/integraciones/manychat', [ManyChatIntegracionController::class, 'index'])->middleware('permission:configuracion.ver')->name('configuracion.manychat.index');
    Route::post('/configuracion/integraciones/manychat/toggle', [ManyChatIntegracionController::class, 'toggle'])->middleware('permission:configuracion.editar')->name('configuracion.manychat.toggle');
    Route::post('/configuracion/integraciones/manychat/regenerar-token', [ManyChatIntegracionController::class, 'regenerarToken'])->middleware('permission:configuracion.editar')->name('configuracion.manychat.regenerar-token');
    Route::post('/configuracion/integraciones/manychat/nombre', [ManyChatIntegracionController::class, 'updateNombre'])->middleware('permission:configuracion.editar')->name('configuracion.manychat.nombre');
    Route::post('/configuracion/integraciones/manychat/plantillas', [ManyChatIntegracionController::class, 'updateTemplates'])->middleware('permission:configuracion.editar')->name('configuracion.manychat.templates');
    Route::get('/configuracion/integraciones/manychat/test', [ManyChatIntegracionController::class, 'testProducto'])->middleware('permission:configuracion.ver')->name('configuracion.manychat.test');
    Route::get('/configuracion/integraciones/wompi', [WompiIntegracionController::class, 'index'])->middleware('permission:configuracion.ver')->name('configuracion.wompi.index');
    Route::post('/configuracion/integraciones/wompi', [WompiIntegracionController::class, 'update'])->middleware('permission:configuracion.editar')->name('configuracion.wompi.update');
    Route::post('/configuracion/integraciones/wompi/generar-link', [WompiIntegracionController::class, 'generateLink'])->middleware('permission:configuracion.editar')->name('configuracion.wompi.generate-link');

    Route::get('/clientes', [ClienteController::class, 'index'])->middleware('permission:clientes.ver')->name('clientes.index');
    Route::post('/clientes', [ClienteController::class, 'store'])->middleware('permission:clientes.crear')->name('clientes.store');
    Route::get('/reportes', [ReporteController::class, 'index'])->middleware('permission:reportes.ver')->name('reportes.index');
    Route::get('/soporte', [SoporteController::class, 'index'])->middleware('permission:dashboard.ver')->name('soporte.index');
    Route::post('/soporte/tickets', [SoporteController::class, 'storeTicket'])->middleware('permission:dashboard.ver')->name('soporte.tickets.store');
    Route::post('/soporte/tickets/{ticket}/comentario', [SoporteController::class, 'addComment'])->middleware('permission:dashboard.ver')->name('soporte.tickets.comment');
    Route::get('/ayuda-ia', [AyudaIaController::class, 'index'])->middleware('permission:dashboard.ver')->name('ayuda-ia.index');
    Route::post('/ayuda-ia/preguntar', [AyudaIaController::class, 'ask'])->middleware('permission:dashboard.ver')->name('ayuda-ia.ask');
    Route::post('/ayuda-ia/reabrir', [AyudaIaController::class, 'reopen'])->middleware('permission:dashboard.ver')->name('ayuda-ia.reopen');

    Route::get('/proveedores', [ProveedorController::class, 'index'])->middleware('permission:proveedores.ver')->name('proveedores.index');
    Route::post('/proveedores', [ProveedorController::class, 'store'])->middleware('permission:proveedores.crear')->name('proveedores.store');

    Route::get('/pos', [PosController::class, 'index'])->middleware('permission:pos.vender')->name('pos.index');
    Route::post('/pos/vender', [PosController::class, 'sell'])->middleware('permission:pos.vender')->name('pos.sell');
    Route::post('/pos/clientes', [PosController::class, 'storeCustomer'])->middleware('permission:pos.vender')->name('pos.customers.store');
    Route::get('/pos/ventas/{venta}/ticket', [PosController::class, 'ticket'])->middleware('permission:ventas.ver')->name('pos.ticket');
    Route::post('/pos/ventas/{venta}/anular', [PosController::class, 'annul'])->middleware('permission:ventas.anular')->name('pos.annul');

    Route::get('/ventas', [VentaController::class, 'index'])->middleware('permission:ventas.ver')->name('ventas.index');
    Route::get('/ventas/{venta}', [VentaController::class, 'show'])->middleware('permission:ventas.ver')->name('ventas.show');
    Route::post('/ventas/{venta}/anular', [VentaController::class, 'annul'])->middleware('permission:ventas.anular')->name('ventas.annul');
    Route::post('/ventas/{venta}/nota-credito', [VentaController::class, 'creditNote'])->middleware('permission:facturacion.notas')->name('ventas.credit-note');
    Route::post('/ventas/{venta}/nota-debito', [VentaController::class, 'debitNote'])->middleware('permission:facturacion.notas')->name('ventas.debit-note');
    Route::get('/ventas/{venta}/factura/imprimir', [VentaController::class, 'printInvoice'])->middleware('permission:ventas.ver')->name('ventas.factura.print');
    Route::get('/ventas/{venta}/ticket/imprimir', [VentaController::class, 'printTicket'])->middleware('permission:ventas.ver')->name('ventas.ticket.print');
    Route::get('/ventas/{venta}/factura/pdf', [VentaController::class, 'downloadInvoicePdf'])->middleware('permission:ventas.ver')->name('ventas.factura.pdf');
    Route::get('/ventas/{venta}/documentos/{factura}/imprimir', [VentaController::class, 'printDocument'])->middleware('permission:ventas.ver')->name('ventas.documents.print');
    Route::get('/ventas/{venta}/documentos/{factura}/pdf', [VentaController::class, 'downloadDocumentPdf'])->middleware('permission:ventas.ver')->name('ventas.documents.pdf');
    Route::post('/ventas/{venta}/documentos/{factura}/anular', [VentaController::class, 'cancelDocument'])->middleware('permission:facturacion.anular_documento')->name('ventas.documents.annul');

    Route::get('/cuentas-cobrar', [CuentasCobrarController::class, 'index'])->middleware('permission:cuentas_cobrar.ver')->name('cuentas-cobrar.index');
    Route::get('/cuentas-cobrar/exportar', [CuentasCobrarController::class, 'exportCsv'])->middleware('permission:cuentas_cobrar.ver')->name('cuentas-cobrar.export.csv');
    Route::get('/cuentas-cobrar/exportar-xlsx', [CuentasCobrarController::class, 'exportXlsx'])->middleware('permission:cuentas_cobrar.ver')->name('cuentas-cobrar.export.xlsx');
    Route::get('/cuentas-cobrar/exportar-pdf', [CuentasCobrarController::class, 'exportPdf'])->middleware('permission:cuentas_cobrar.ver')->name('cuentas-cobrar.export.pdf');
    Route::post('/cuentas-cobrar/{venta}/abonar', [CuentasCobrarController::class, 'collect'])->middleware('permission:cuentas_cobrar.abonar')->name('cuentas-cobrar.collect');

    Route::get('/caja', [CajaController::class, 'index'])->middleware('permission:caja.ver')->name('caja.index');
    Route::post('/caja/abrir', [CajaController::class, 'open'])->middleware('permission:caja.abrir')->name('caja.open');
    Route::post('/caja/movimientos', [CajaController::class, 'movement'])->middleware('permission:caja.ver')->name('caja.movements.store');
    Route::post('/caja/traspasos', [CajaController::class, 'transfer'])->middleware('permission:caja.ver')->name('caja.transfers.store');
    Route::post('/caja/traspasos/{traspaso}/aprobar', [CajaController::class, 'approveTransfer'])->middleware('permission:caja.ver')->name('caja.transfers.approve');
    Route::post('/caja/traspasos/{traspaso}/rechazar', [CajaController::class, 'rejectTransfer'])->middleware('permission:caja.ver')->name('caja.transfers.reject');
    Route::post('/caja/cerrar', [CajaController::class, 'close'])->middleware('permission:caja.cerrar')->name('caja.close');

    Route::get('/categorias', [CategoriaController::class, 'index'])->middleware('permission:categorias.ver')->name('categorias.index');
    Route::post('/categorias', [CategoriaController::class, 'store'])->middleware('permission:categorias.crear')->name('categorias.store');
    Route::get('/categorias/{categoria}/editar', [CategoriaController::class, 'edit'])->middleware('permission:categorias.ver')->name('categorias.edit');
    Route::put('/categorias/{categoria}', [CategoriaController::class, 'update'])->middleware('permission:categorias.crear')->name('categorias.update');
    Route::patch('/categorias/{categoria}/desactivar', [CategoriaController::class, 'deactivate'])->middleware('permission:categorias.crear')->name('categorias.deactivate');
    Route::patch('/categorias/{categoria}/reactivar', [CategoriaController::class, 'reactivate'])->middleware('permission:categorias.crear')->name('categorias.reactivate');
    Route::delete('/categorias/{categoria}', [CategoriaController::class, 'destroy'])->middleware('permission:categorias.crear')->name('categorias.destroy');

    Route::get('/productos', [ProductoController::class, 'index'])->middleware('permission:productos.ver')->name('productos.index');
    Route::post('/productos', [ProductoController::class, 'store'])->middleware('permission:productos.crear')->name('productos.store');
    Route::get('/productos/plantilla', [ProductoController::class, 'downloadTemplate'])->middleware('permission:productos.ver')->name('productos.template');
    Route::post('/productos/importar', [ProductoController::class, 'import'])->middleware('permission:productos.crear')->name('productos.import');
    Route::get('/productos/{producto}/editar', [ProductoController::class, 'edit'])->middleware('permission:productos.ver')->name('productos.edit');
    Route::put('/productos/{producto}', [ProductoController::class, 'update'])->middleware('permission:productos.crear')->name('productos.update');
    Route::patch('/productos/{producto}/desactivar', [ProductoController::class, 'deactivate'])->middleware('permission:productos.crear')->name('productos.deactivate');
    Route::patch('/productos/{producto}/reactivar', [ProductoController::class, 'reactivate'])->middleware('permission:productos.crear')->name('productos.reactivate');
    Route::delete('/productos/{producto}', [ProductoController::class, 'destroy'])->middleware('permission:productos.crear')->name('productos.destroy');

    Route::get('/inventario', [InventarioController::class, 'index'])->middleware('permission:inventario.ver')->name('inventario.index');
    Route::post('/inventario/movimientos', [InventarioController::class, 'storeMovement'])->middleware('permission:inventario.ajustar')->name('inventario.movements.store');
    Route::post('/inventario/traslados', [InventarioController::class, 'storeTransfer'])->middleware('permission:inventario.ajustar')->name('inventario.transfers.store');
    Route::post('/inventario/ajustes', [InventarioController::class, 'storeAdjustmentRequest'])->middleware('permission:inventario.ajustar')->name('inventario.adjustments.store');
    Route::post('/inventario/ajustes/{ajuste}/aprobar', [InventarioController::class, 'approveAdjustment'])->middleware('permission:inventario.ajustar')->name('inventario.adjustments.approve');
    Route::post('/inventario/ajustes/{ajuste}/rechazar', [InventarioController::class, 'rejectAdjustment'])->middleware('permission:inventario.ajustar')->name('inventario.adjustments.reject');

    Route::get('/compras', [CompraController::class, 'index'])->middleware('permission:compras.ver')->name('compras.index');
    Route::post('/compras', [CompraController::class, 'store'])->middleware('permission:compras.crear')->name('compras.store');
    Route::post('/compras/{compra}/pagar', [CompraController::class, 'pay'])->middleware('permission:compras.pagar')->name('compras.pay');
    Route::post('/compras/{compra}/anular', [CompraController::class, 'annul'])->middleware('permission:compras.anular')->name('compras.annul');

    Route::get('/compras/ordenes', [OrdenCompraController::class, 'index'])->middleware('permission:compras.ordenes')->name('compras.ordenes.index');
    Route::post('/compras/ordenes', [OrdenCompraController::class, 'store'])->middleware('permission:compras.ordenes')->name('compras.ordenes.store');
    Route::get('/compras/ordenes/{orden}/pdf', [CompraController::class, 'orderPdf'])->middleware('permission:compras.ver')->name('compras.ordenes.pdf');
    Route::get('/compras/ordenes/{orden}/recibir', [OrdenCompraController::class, 'showReceive'])->middleware('permission:compras.recibir')->name('compras.ordenes.receive');
    Route::post('/compras/ordenes/{orden}/recibir', [OrdenCompraController::class, 'storeReceive'])->middleware('permission:compras.recibir')->name('compras.ordenes.receive.store');

    // Configuración › Envíos
    Route::get('/configuracion/envios', [ConfiguracionController::class, 'enviosIndex'])->middleware('permission:configuracion.ver')->name('configuracion.envios');
    Route::post('/configuracion/envios', [ConfiguracionController::class, 'enviosUpdate'])->middleware('permission:configuracion.editar')->name('configuracion.envios.update');

    // Envíos y rótulos
    Route::get('/envios', [EnvioController::class, 'index'])->middleware('permission:envios.ver')->name('envios.index');
    Route::get('/envios/crear', [EnvioController::class, 'create'])->middleware('permission:envios.crear')->name('envios.create');
    Route::post('/envios', [EnvioController::class, 'store'])->middleware('permission:envios.crear')->name('envios.store');
    Route::get('/envios/{envio}', [EnvioController::class, 'show'])->middleware('permission:envios.ver')->name('envios.show');
    Route::get('/envios/{envio}/editar', [EnvioController::class, 'edit'])->middleware('permission:envios.crear')->name('envios.edit');
    Route::put('/envios/{envio}', [EnvioController::class, 'update'])->middleware('permission:envios.crear')->name('envios.update');
    Route::patch('/envios/{envio}/estado', [EnvioController::class, 'updateEstado'])->middleware('permission:envios.cambiar_estado')->name('envios.estado');
    Route::delete('/envios/{envio}', [EnvioController::class, 'destroy'])->middleware('permission:envios.eliminar')->name('envios.destroy');
    Route::post('/envios/{envio}/rotulos', [EnvioController::class, 'generarRotulo'])->middleware('permission:rotulos.generar')->name('envios.rotulos.generar');
    Route::post('/envios/{envio}/rotulos-masivo', [EnvioController::class, 'generarMasivo'])->middleware('permission:rotulos.generar')->name('envios.rotulos.masivo');
    Route::get('/rotulos/{rotulo}/imprimir', [EnvioController::class, 'printRotulo'])->middleware('permission:rotulos.imprimir')->name('rotulos.print');
    Route::get('/rotulos/{rotulo}/pdf', [EnvioController::class, 'downloadRotuloPdf'])->middleware('permission:rotulos.imprimir')->name('rotulos.pdf');
    Route::delete('/rotulos/{rotulo}', [EnvioController::class, 'destroyRotulo'])->middleware('permission:rotulos.generar')->name('rotulos.destroy');
});

Route::view('/health-check', 'health-check')->name('health-check');

// Página pública de precios
Route::get('/precios', [PlanController::class, 'publicPricing'])->name('precios');
