<?php

declare(strict_types=1);

use App\Core\Router;

return static function (Router $router): void {
    $router->get('/', static function (): never {
        redirect('login');
    });

    $router->post('/webhooks/wompi', 'SubscriptionController@webhook', ['no_csrf']);
    $router->post('/webhooks/bold', 'IntegrationController@boldWebhook', ['no_csrf']);
    $router->post('/webhooks/credibanco', 'IntegrationController@credibancoWebhook', ['no_csrf']);
    $router->get('/empresa/{slug}', 'SubscriptionController@accessBySlug');
    $router->get('/suscripcion/resultado', 'SubscriptionController@result');
    $router->get('/suscripcion/estado', 'SubscriptionController@status');

    $router->group('', static function (Router $router): void {
        $router->get('/login', 'AuthController@showLogin');
        $router->post('/login', 'AuthController@login');

        $router->get('/registro-empresa', 'CompanyController@showRegister');
        $router->post('/registro-empresa', 'CompanyController@register');
    }, ['guest']);

    $router->group('', static function (Router $router): void {
        $router->post('/logout', 'AuthController@logout');
        $router->get('/perfil', 'ProfileController@index');
        $router->post('/perfil', 'ProfileController@update');
        $router->post('/perfil/password', 'ProfileController@updatePassword');

        $router->get('/dashboard', 'DashboardController@index', ['permission:dashboard.ver']);
        $router->get('/notificaciones/stock-minimo', 'NotificationController@stockMinimum', ['tenant']);
        $router->get('/superadmin/notificaciones/soporte', 'NotificationController@superadminSupport', ['superadmin']);
        $router->post('/notificaciones/marcar-leidas', 'NotificationController@markRead');

        $router->get('/empresas', 'CompanyController@index', ['permission:empresas.ver']);
        $router->get('/empresas/seleccionar', 'CompanyController@select');
        $router->post('/empresas/seleccionar', 'CompanyController@switch');

        $router->get('/sucursales', 'BranchController@index', ['tenant', 'permission:sucursales.ver']);
        $router->post('/sucursales', 'BranchController@store', ['tenant', 'permission:sucursales.crear']);
        $router->post('/sucursales/{id}/actualizar', 'BranchController@update', ['tenant', 'permission:sucursales.editar']);
        $router->post('/sucursales/{id}/estado', 'BranchController@toggle', ['tenant', 'permission:sucursales.editar']);
        $router->post('/sucursales/{id}/usuarios', 'BranchController@assignUser', ['tenant', 'permission:sucursales.editar']);
        $router->post('/sucursales/{id}/usuarios/{userId}/quitar', 'BranchController@unassignUser', ['tenant', 'permission:sucursales.editar']);
        $router->post('/sucursales/{id}/cajas', 'BranchController@storeCashBox', ['tenant', 'permission:sucursales.editar']);
        $router->post('/sucursales/cajas/{id}/estado', 'BranchController@toggleCashBox', ['tenant', 'permission:sucursales.editar']);

        $router->get('/usuarios', 'UserController@index', ['tenant', 'permission:usuarios.ver']);
        $router->post('/usuarios', 'UserController@store', ['tenant', 'permission:usuarios.crear']);
        $router->post('/usuarios/roles/permisos', 'UserController@updateRolePermissions', ['tenant', 'permission:roles.gestionar']);

        $router->get('/categorias', 'CategoryController@index', ['tenant', 'permission:categorias.ver']);
        $router->post('/categorias', 'CategoryController@store', ['tenant', 'permission:categorias.crear']);

        $router->get('/productos', 'ProductController@index', ['tenant', 'permission:productos.ver']);
        $router->post('/productos', 'ProductController@store', ['tenant', 'permission:productos.crear']);
        $router->post('/productos/{id}/actualizar', 'ProductController@update', ['tenant', 'permission:productos.crear']);
        $router->get('/productos/plantilla', 'ProductController@downloadTemplate', ['tenant', 'permission:productos.ver']);
        $router->post('/productos/importar', 'ProductController@import', ['tenant', 'permission:productos.crear']);

        $router->get('/inventario', 'InventoryController@index', ['tenant', 'permission:inventario.ver']);
        $router->post('/inventario/traslados', 'InventoryController@transfer', ['tenant', 'permission:inventario.ver']);
        $router->post('/inventario/ajustes', 'InventoryController@storeAdjustment', ['tenant', 'permission:inventario.ver']);
        $router->post('/inventario/ajustes/{id}/aprobar', 'InventoryController@approveAdjustment', ['tenant', 'permission:inventario.ver']);
        $router->post('/inventario/ajustes/{id}/rechazar', 'InventoryController@rejectAdjustment', ['tenant', 'permission:inventario.ver']);

        $router->get('/proveedores', 'SupplierController@index', ['tenant', 'permission:proveedores.ver']);
        $router->post('/proveedores', 'SupplierController@store', ['tenant', 'permission:proveedores.crear']);

        $router->get('/compras', 'PurchaseController@index', ['tenant', 'permission:compras.ver']);
        $router->post('/compras', 'PurchaseController@store', ['tenant', 'permission:compras.crear']);
        $router->post('/compras/ordenes', 'PurchaseController@storeOrder', ['tenant', 'permission:compras.crear']);
        $router->get('/compras/ordenes/{id}/pdf', 'PurchaseController@downloadOrderPdf', ['tenant', 'permission:compras.ver']);
        $router->post('/compras/ordenes/{id}/recibir', 'PurchaseController@receiveOrder', ['tenant', 'permission:compras.crear']);
        $router->post('/compras/{id}/pagar', 'PurchaseController@pay', ['tenant', 'permission:compras.pagar']);
        $router->post('/compras/{id}/anular', 'PurchaseController@annul', ['tenant', 'permission:compras.anular']);

        $router->get('/clientes', 'CustomerController@index', ['tenant', 'permission:clientes.ver']);
        $router->post('/clientes', 'CustomerController@store', ['tenant', 'permission:clientes.crear']);

        $router->get('/caja', 'CashController@index', ['tenant', 'permission:caja.ver']);
        $router->post('/caja/abrir', 'CashController@open', ['tenant', 'permission:caja.abrir']);
        $router->post('/caja/movimientos', 'CashController@movement', ['tenant', 'permission:caja.cerrar']);
        $router->post('/caja/traspasos', 'CashController@transfer', ['tenant', 'permission:caja.cerrar']);
        $router->post('/caja/traspasos/{id}/aprobar', 'CashController@approveTransfer', ['tenant', 'permission:caja.cerrar']);
        $router->post('/caja/traspasos/{id}/rechazar', 'CashController@rejectTransfer', ['tenant', 'permission:caja.cerrar']);
        $router->get('/caja/traspasos/pending-count', 'CashController@pendingTransfersCount', ['tenant', 'permission:caja.ver']);
        $router->post('/caja/cerrar', 'CashController@close', ['tenant', 'permission:caja.cerrar']);
        $router->get('/caja/cierres/{id}/imprimir', 'CashController@printClosing', ['tenant', 'permission:caja.ver']);

        $router->get('/pos', 'PosController@index', ['tenant', 'permission:pos.vender']);
        $router->post('/pos/vender', 'PosController@sell', ['tenant', 'permission:pos.vender']);
        $router->post('/pos/clientes', 'PosController@storeCustomer', ['tenant', 'permission:pos.vender']);

        $router->get('/ventas', 'SaleController@index', ['tenant', 'permission:ventas.ver']);
        $router->get('/ventas/{id}', 'SaleController@show', ['tenant', 'permission:ventas.ver']);
        $router->get('/ventas/{id}/factura/imprimir', 'SaleController@printInvoice', ['tenant', 'permission:ventas.ver']);
        $router->get('/ventas/{id}/factura/pdf', 'SaleController@downloadInvoicePdf', ['tenant', 'permission:ventas.ver']);
        $router->get('/ventas/{id}/ticket/imprimir', 'SaleController@printTicket', ['tenant', 'permission:ventas.ver']);
        $router->post('/ventas/{id}/nota-credito', 'SaleController@creditNote', ['tenant', 'permission:facturacion.notas']);
        $router->post('/ventas/{id}/nota-debito', 'SaleController@debitNote', ['tenant', 'permission:facturacion.notas']);
        $router->post('/ventas/{id}/documentos/{documentId}/anular', 'SaleController@cancelDocument', ['tenant', 'permission:facturacion.anular_documento']);
        $router->get('/ventas/{id}/documentos/{documentId}/imprimir', 'SaleController@printDocument', ['tenant', 'permission:ventas.ver']);
        $router->get('/ventas/{id}/documentos/{documentId}/pdf', 'SaleController@downloadDocumentPdf', ['tenant', 'permission:ventas.ver']);
        $router->post('/ventas/{id}/anular', 'SaleController@annul', ['tenant', 'permission:ventas.anular']);

        $router->get('/reportes', 'ReportController@index', ['tenant', 'permission:reportes.ver']);

        $router->get('/soporte', 'SupportController@index', ['tenant']);
        $router->post('/soporte/tickets', 'SupportController@storeTicket', ['tenant']);
        $router->post('/soporte/tickets/{id}/comentario', 'SupportController@addComment', ['tenant']);
        $router->get('/ayuda-ia', 'AiHelpController@index', ['tenant']);
        $router->post('/ayuda-ia/preguntar', 'AiHelpController@ask', ['tenant']);
        $router->post('/ayuda-ia/reabrir', 'AiHelpController@reopen', ['tenant']);

        $router->get('/configuracion', 'ConfigurationController@index', ['tenant', 'permission:configuracion.ver']);
        $router->get('/integraciones', 'IntegrationController@index', ['tenant', 'permission:configuracion.ver']);
        $router->post('/integraciones/bold', 'IntegrationController@updateBold', ['tenant', 'permission:configuracion.editar']);
        $router->post('/integraciones/bold/probar', 'IntegrationController@testBoldTransaction', ['tenant', 'permission:configuracion.editar']);
        $router->post('/integraciones/bold/probar-boton', 'IntegrationController@testBoldButtonCheckout', ['tenant', 'permission:configuracion.editar']);
        $router->post('/integraciones/credibanco', 'IntegrationController@updateCredibanco', ['tenant', 'permission:configuracion.editar']);
        $router->post('/integraciones/credibanco/probar', 'IntegrationController@testCredibancoTransaction', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/empresa', 'ConfigurationController@updateCompany', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/fiscal', 'ConfigurationController@updateFiscal', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/fiscal/facturacion-electronica', 'ConfigurationController@updateElectronicBilling', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/fiscal/factus/probar', 'ConfigurationController@testFactusConnection', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/fiscal/factus/probar-factura', 'ConfigurationController@testFactusInvoice', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/fiscal/factus/rangos', 'ConfigurationController@loadFactusRanges', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/fiscal/factus/municipios', 'ConfigurationController@loadFactusMunicipalities', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/impuestos', 'ConfigurationController@storeTax', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/impuestos/{id}/estado', 'ConfigurationController@toggleTax', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/metodos-pago', 'ConfigurationController@storePaymentMethod', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/metodos-pago/{id}/estado', 'ConfigurationController@togglePaymentMethod', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/unidades-medida', 'ConfigurationController@storeMeasurementUnit', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/unidades-medida/{id}/actualizar', 'ConfigurationController@updateMeasurementUnit', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/unidades-medida/{id}/estado', 'ConfigurationController@toggleMeasurementUnit', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/unidades-medida/{id}/eliminar', 'ConfigurationController@deleteMeasurementUnit', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/numeraciones', 'ConfigurationController@storeDocumentNumbering', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/numeraciones/{id}/estado', 'ConfigurationController@toggleDocumentNumbering', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/resoluciones', 'ConfigurationController@storeDianResolution', ['tenant', 'permission:configuracion.editar']);
        $router->post('/configuracion/resoluciones/{id}/estado', 'ConfigurationController@toggleDianResolution', ['tenant', 'permission:configuracion.editar']);

        $router->get('/cuentas-cobrar', 'ReceivableController@index', ['tenant', 'permission:ventas.ver']);
        $router->get('/cuentas-cobrar/exportar', 'ReceivableController@export', ['tenant', 'permission:ventas.ver']);
        $router->get('/cuentas-cobrar/exportar-xlsx', 'ReceivableController@exportXlsx', ['tenant', 'permission:ventas.ver']);
        $router->get('/cuentas-cobrar/exportar-pdf', 'ReceivableController@exportPdf', ['tenant', 'permission:ventas.ver']);
        $router->post('/cuentas-cobrar/{id}/abonar', 'ReceivableController@collect', ['tenant', 'permission:ventas.ver']);

        $router->get('/superadmin', 'SuperadminController@index', ['superadmin']);
        $router->post('/superadmin/planes', 'SuperadminController@storePlan', ['superadmin']);
        $router->post('/superadmin/planes/{id}/actualizar', 'SuperadminController@updatePlan', ['superadmin']);
        $router->post('/superadmin/planes/{id}/eliminar', 'SuperadminController@deletePlan', ['superadmin']);
        $router->post('/superadmin/suscripciones/automatizacion/ejecutar', 'SuperadminController@runSubscriptionAutomation', ['superadmin']);
        $router->post('/superadmin/suscripciones/avisos/reintentar', 'SuperadminController@retryCollectionNotices', ['superadmin']);
        $router->post('/superadmin/checkout-intents/{id}/reprocesar', 'SuperadminController@reprocessCheckoutIntent', ['superadmin']);
        $router->post('/superadmin/wompi-config', 'SuperadminController@updateWompiConfig', ['superadmin']);
        $router->post('/superadmin/wompi-config/probar', 'SuperadminController@testWompiConnection', ['superadmin']);
        $router->post('/superadmin/wompi-config/limpiar', 'SuperadminController@clearWompiKey', ['superadmin']);
        $router->post('/superadmin/correo-config', 'SuperadminController@updateSmtpConfig', ['superadmin']);
        $router->post('/superadmin/correo-config/probar', 'SuperadminController@testSmtpConnection', ['superadmin']);
        $router->post('/superadmin/notificaciones-config', 'SuperadminController@updateNotificationConfig', ['superadmin']);
        $router->post('/superadmin/notificaciones-config/probar-whatsapp', 'SuperadminController@testNotificationWhatsApp', ['superadmin']);
        $router->post('/superadmin/notificaciones-config/limpiar', 'SuperadminController@clearNotificationKey', ['superadmin']);
        $router->post('/superadmin/whatsapp-meta-templates', 'SuperadminController@storeWhatsAppMetaTemplate', ['superadmin']);
        $router->post('/superadmin/whatsapp-meta-templates/sincronizar-meta', 'SuperadminController@syncWhatsAppMetaTemplateCatalog', ['superadmin']);
        $router->post('/superadmin/whatsapp-meta-templates/{id}/sincronizar', 'SuperadminController@syncWhatsAppMetaTemplateStatus', ['superadmin']);
        $router->post('/superadmin/whatsapp-meta-templates/{id}/enlazar', 'SuperadminController@bindWhatsAppMetaTemplate', ['superadmin']);
        $router->post('/superadmin/whatsapp-meta-templates/programacion', 'SuperadminController@updateWhatsAppTemplateDispatchTimes', ['superadmin']);
        $router->post('/superadmin/whatsapp-meta-templates/{id}/enviar-ahora', 'SuperadminController@sendWhatsAppMetaTemplateNow', ['superadmin']);
        $router->post('/superadmin/ia-config', 'SuperadminController@updateAiConfig', ['superadmin']);
        $router->post('/superadmin/ia-config/limpiar', 'SuperadminController@clearAiKey', ['superadmin']);
        $router->post('/superadmin/ia-config/probar', 'SuperadminController@testAiAssistant', ['superadmin']);
        $router->post('/superadmin/reporte-openia-config', 'SuperadminController@updateOpenAiReportConfig', ['superadmin']);
        $router->post('/superadmin/soporte/tickets', 'SuperadminController@storeSupportTicket', ['superadmin']);
        $router->post('/superadmin/soporte/tickets/{id}/actualizar', 'SuperadminController@updateSupportTicket', ['superadmin']);
        $router->post('/superadmin/soporte/tickets/{id}/comentario', 'SuperadminController@addSupportTicketComment', ['superadmin']);
        $router->post('/superadmin/empresas/{id}/estado', 'SuperadminController@updateCompanyStatus', ['superadmin']);
        $router->post('/superadmin/empresas/{id}/eliminar', 'SuperadminController@deleteCompany', ['superadmin']);
        $router->post('/superadmin/empresas/{id}/factus', 'SuperadminController@updateCompanyFactus', ['superadmin']);
        $router->post('/superadmin/empresas/{id}/factus/probar', 'SuperadminController@testCompanyFactusConnection', ['superadmin']);
        $router->post('/superadmin/empresas/{id}/factus/probar-factura', 'SuperadminController@testCompanyFactusInvoice', ['superadmin']);
        $router->post('/superadmin/empresas/{id}/factus/rangos', 'SuperadminController@loadCompanyFactusRanges', ['superadmin']);
        $router->post('/superadmin/empresas/{id}/factus/municipios', 'SuperadminController@loadCompanyFactusMunicipalities', ['superadmin']);
        $router->post('/superadmin/suscripciones/{id}/actualizar', 'SuperadminController@updateSubscription', ['superadmin']);
        $router->post('/superadmin/pagos', 'SuperadminController@storePayment', ['superadmin']);
    }, ['auth']);

    // Alias corto de acceso por empresa: /{slug}
    // Va al final para no interceptar rutas funcionales existentes.
    $router->get('/{slug}', 'SubscriptionController@accessBySlug');
};
