<?php

use App\Controllers\Auth;
use App\Controllers\Dashboard;
use App\Controllers\UserController;
use App\Controllers\BrandController;
use App\Controllers\GroupController;
use App\Controllers\OrderController;
use App\Controllers\StoreController;
use App\Controllers\QrCodeController;
use App\Controllers\ReportController;
use App\Controllers\AttribuController;
use App\Controllers\AvanceController;
use App\Controllers\CompanyController;
use App\Controllers\ProductCOntroller;
use App\Controllers\CategoryController;
use App\Controllers\MecanicienController;
use App\Controllers\NotificationController;
use App\Controllers\StatistiqueController;
use App\Controllers\RecouvrementController;
use App\Controllers\ReservationController;
use App\Controllers\SecuriteController;
use App\Controllers\SortieCaisseController;
use App\Controllers\RemiseController;
use App\Controllers\PerformanceController;
/**
 * auth route
 * the option array filter make a filter, 
 * if the user are already loged in, redirect to dashboard
 */
$routes->get('/login', [Auth::class, 'login'], ['filter' => 'loggedIn']);
$routes->post('/login', [Auth::class, 'loginPost'], ['filter' => 'loggedIn']);
$routes->get('test-email', 'TestEmail::index');
$routes->get('alerts/check', 'AlertsController::check');
$routes->get('check-deadline', 'TestDeadline::index');






/**
 * route to all the rest of web app
 * the filter do if user not connectet try to acces this route
 * redirect imediatly iny login
 */
$routes->group('', ['filter' => 'auth'], function ($routes) {

    /**
     * dashboard route
     */
    $routes->get('/', [Dashboard::class, 'index']);
    $routes->get('/ventes', [Auth::class, 'ventes']);
    $routes->get('/ventes/(:num)', [Auth::class, 'addImage']);
    $routes->get('/ventes/fetchProductVente/(:num)', [Auth::class, 'fetchProductVente']);
    $routes->get('/ventes/show/(:num)', [Auth::class, 'getSingle']);
    $routes->post('/ventes/moreimage/(:num)', [Auth::class, 'uploadImagePub']);
    $routes->post('/ventes/moreimage/supp/(:num)', [Auth::class, 'delete']);
    

    /**
     * route to logout
     */
    $routes->get('/logout', [Auth::class, 'logout']);
    // Route pour tester les alertes manuellement (à supprimer en production)
    $routes->get('test-deadline-alerts', 'AvanceController::checkDeadlineAlerts');

    /**
     * route for the users
     */
    $routes->group('/users', function ($routes) {
        $routes->get('/', [UserController::class, 'index']);
        $routes->get('create', [UserController::class, 'create']);
        $routes->post('create', [UserController::class, 'create']);
        $routes->get('edit/(:num)', [UserController::class, 'edit']);
        $routes->post('edit/(:num)', [UserController::class, 'edit']);
        $routes->get('delete/(:num)', [UserController::class, 'delete']);
        $routes->post('delete/(:num)', [UserController::class, 'delete']);
        $routes->get('profile', [UserController::class, 'profile']);
        $routes->get('fetchProfile/(:num)', [UserController::class, 'fetchProfile']);
        $routes->get('setting', [UserController::class, 'setting']);
        $routes->post('setting', [UserController::class, 'setting']);
        $routes->post('assign_store', [UserController::class, 'assign_store']);
        $routes->get('login/(:num)', [UserController::class, 'loginToOtherAccount']);
        $routes->get('fetchUserData', [UserController::class,'fetchUserData']);
        $routes->post('fetchUserForAssign', [UserController::class,'fetchUserForAssign']);
    });

    /**
     * route for the groups
     */
    $routes->group('/groups', function ($routes) {
        $routes->get('/', [GroupController::class, 'index']);
        $routes->get('create', [GroupController::class, 'create']);
        $routes->post('create', [GroupController::class, 'create']);
        $routes->get('edit/(:num)', [GroupController::class, 'edit']);
        $routes->post('edit/(:num)', [GroupController::class, 'edit']);
        $routes->get('delete/(:num)', [GroupController::class, 'delete']);
        $routes->post('delete/(:num)', [GroupController::class, 'delete']);
    });

    /**
     * route for the brands
     */
    $routes->group('/brands', function ($routes) {
        $routes->get('/', [BrandController::class, 'index']);
        $routes->get('fetchBrandData', [BrandController::class, 'fetchBrandData']);
        $routes->post('create', [BrandController::class, 'create']);
        $routes->post('fetchBrandDataById/(:num)', [BrandController::class, 'fetchBrandDataById']);
        $routes->post('update/(:num)', [BrandController::class, 'update']);
        $routes->post('remove', [BrandController::class, 'remove']);
    });

    /**
     * route for the category
     */
    $routes->group('/category', function ($routes) {
        $routes->get('/', [CategoryController::class, 'index']);
        $routes->get('fetchCategoryData', [CategoryController::class, 'fetchCategoryData']);
        $routes->post('create', [CategoryController::class, 'create']);
        $routes->post('fetchCategoryDataById/(:num)', [CategoryController::class, 'fetchCategoryDataById']);
        $routes->post('update/(:num)', [CategoryController::class, 'update']);
        $routes->post('remove', [CategoryController::class, 'remove']);
    });

    /**
     * route for the stores
     */
    $routes->group('/stores', function ($routes) {
        $routes->get('/', [StoreController::class, 'index']);
        $routes->get('fetchStoresData', [StoreController::class, 'fetchStoresData']);
        $routes->post('fetchStoresDataById/(:num)', [StoreController::class, 'fetchStoresDataById']);
        $routes->post('create', [StoreController::class, 'create']);
        $routes->post('update/(:num)', [StoreController::class, 'update']);
        $routes->post('remove/', [StoreController::class, 'remove']);
    });

    /**
     * route for the Securite
     */
    $routes->group('/validateSecurite', function ($routes) {
        $routes->get('/', [SecuriteController::class, 'index']);
        $routes->get('fetchSecuriteData', [SecuriteController::class, 'fetchSecuriteData']);
        $routes->post('fetchSecuriteDataById/(:num)', [SecuriteController::class, 'fetchSecuriteDataById']);
        $routes->post('update/(:num)', [SecuriteController::class, 'update']);
    });

    /**
     * route for the attributes
     */
    $routes->group('/attributes', function ($routes) {
        $routes->get('/', [AttribuController::class, 'index']);
        $routes->get('values/(:num)', [AttribuController::class, 'getValue']);
        $routes->get('fetchAttributeData', [AttribuController::class, 'fetchCategoryData']);
        $routes->get('fetchAttributeValueData/(:num)', [AttribuController::class, 'fetchCategoryValueData']);
        $routes->post('fetchAttributeDataById/(:num)', [AttribuController::class, 'fetchAttributeDataById']);
        $routes->post('fetchAttributeValueById/(:num)', [AttribuController::class, 'fetchAttributeValueById']);
        $routes->post('create', [AttribuController::class, 'create']);
        $routes->post('update/(:num)', [AttribuController::class, 'update']);
        $routes->post('remove', [AttribuController::class, 'remove']);
        $routes->post('createValue', [AttribuController::class, 'createValue']);
        $routes->post('updateValue/(:num)', [AttribuController::class, 'updateValue']);
        $routes->post('removeValue', [AttribuController::class, 'removeValue']);
    });

    /**
     * route for the products
     */
    $routes->get('/product', function() {
        return redirect()->to('/products');
    });
    
    // Route pour /product/(:any) qui redirige vers /products/(:any)
    $routes->get('/product/(:any)', function($segment) {
        return redirect()->to('/products/'.$segment);
    });
    
    $routes->group('/products', function ($routes) {
        $routes->get('/', [ProductCOntroller::class, 'index']);
        $routes->get('fetchProductData', [ProductCOntroller::class, 'fetchProductData']);
        $routes->get('create', [ProductCOntroller::class, 'create']);
        $routes->post('create', [ProductCOntroller::class, 'create']);
        $routes->get('update/(:num)', [ProductCOntroller::class, 'update']);
        $routes->post('update/(:num)', [ProductCOntroller::class, 'update']);
        $routes->post('remove', [ProductCOntroller::class, 'remove']);
        $routes->get('generateqrcode/(:num)', [QrCodeCOntroller::class, 'generate']);
        $routes->post('assign_store', [ProductCOntroller::class, 'assign_store']);
        $routes->post('createByExcel', [ProductCOntroller::class, 'createByExcel']);
        $routes->post('checkProductAvailability', [ProductCOntroller::class, 'checkProductAvailability']);
    });

    /**
     * route for the orders
     */
    $routes->group('/orders', function ($routes) {
        $routes->get('/', [OrderController::class, 'index']);
        $routes->get('fetchOrdersData', [OrderController::class, 'fetchOrdersData']);
        $routes->get('editAsNew/(:num)', 'OrderController::editAsNew/$1');
        $routes->get('create', [OrderController::class, 'create']);
        $routes->post('create', [OrderController::class, 'create']);
        $routes->post('getProductValueById', [OrderController::class, 'getProductValueById']);
        $routes->post('getTableProductRow', [OrderController::class, 'getTableProductRow']);
        $routes->get('update/(:num)', [OrderController::class, 'update']);
        $routes->post('update/(:num)', [OrderController::class, 'update']);
        $routes->get('printDiv/(:num)', [OrderController::class, 'print5']);
        $routes->get('printDivBL/(:num)', [OrderController::class, 'print7']);
        $routes->get('printDivBLF/(:num)', [OrderController::class, 'print31']);
        $routes->post('remove', [OrderController::class, 'remove']);
        $routes->post('markAsDelivered', [OrderController::class, 'markAsDelivered']);
        $routes->get('lookOrder/(:num)', [OrderController::class, 'lookOrder']);
        $routes->get('createFromEspace/(:num)', [OrderController::class, 'createById']);
        $routes->get('resrevation', [ReservationController::class, 'index']);
    

    });
    
    /**
     * route for the reports
     */

    $routes->group('reports', function ($routes) {
    
        $routes->get('/', [ReportController::class, 'index']);
        $routes->post('/', [ReportController::class, 'index']);
    
        $routes->get('detail/stock', [ReportController::class, 'stockDetail']);
    
        // Corrections fetct → fetch
        $routes->get('detail/fetchData/(:num)', [ReportController::class, 'fetchProductSold/$1']);
        $routes->get('detail/fetchDataStock/(:num)', [ReportController::class, 'fetchProductStock/$1']);
        $routes->get('detail/fetchDataStock2/(:num)', [ReportController::class, 'fetchProductStock2/$1']);
    
        $routes->get('detail/performance', [ReportController::class, 'performanceDetail']);
        $routes->get('detail/fetchPerformances', [ReportController::class, 'fetchPerformances']);
        $routes->get('detail/fetchCaissierPerformances', [ReportController::class, 'fetchCaissierPerformances']);
    
        // Sécurité (correction du chemin et du contrôleur)
        $routes->get('detail/fetchSecuritePerformances', [ReportController::class, 'fetchSecuritePerformances']);
        $routes->get('detail/getSecuriteValidationDetails/(:num)', [ReportController::class, 'getSecuriteValidationDetails']);
    
        $routes->get('securite-history', [ReportController::class, 'securiteHistory'], ['filter' => 'auth']);
    });

    //autres encaissement 
    $routes->group('encaissements', ['filter' => 'auth'], function($routes) {
        // Page principale
        $routes->get('/', 'AutresEncaissementsController::index');
        
        // Créer un encaissement
        $routes->post('create', 'AutresEncaissementsController::create');
        
        // Récupérer les données pour DataTables
        $routes->get('fetch', 'AutresEncaissementsController::fetchEncaissements');
        
        // Détails d'un encaissement
        $routes->get('details/(:num)', 'AutresEncaissementsController::getDetails/$1');
        
        // Récupérer un encaissement pour édition
        $routes->get('getEncaissement/(:num)', 'AutresEncaissementsController::getEncaissement/$1');
        
        // Modifier un encaissement
        $routes->post('update/(:num)', 'AutresEncaissementsController::update/$1');
        
        // Supprimer un encaissement
        $routes->post('delete/(:num)', 'AutresEncaissementsController::delete/$1');
        
        // Statistiques
        $routes->get('statistics', 'AutresEncaissementsController::getStatistics');
    });

    /**
     * route for the company
     */
    $routes->group('/company', function ($routes) {
        $routes->get('/', [CompanyController::class, 'index']);
        $routes->post('/', [CompanyController::class, 'index']);
    });

    /**
     * route for the users statistic
     */
    $routes->group('/statistic', function ($routes) {
        $routes->get('/', [StatistiqueController::class, 'index']);
        $routes->get('(:num)', [StatistiqueController::class, 'singleStat']);
    });
        // routes for recouvrement
        $routes->group('/recouvrement', function ($routes) {
            $routes->get('/', [RecouvrementController::class, 'index']);
            $routes->get('fetchRecouvrementData', [RecouvrementController::class, 'fetchRecouvrementData']);
            $routes->get('fetchTotalRecouvrementData', [RecouvrementController::class, 'fetchTotalRecouvrementData']);
            $routes->get('fetchTotalData', [RecouvrementController::class, 'fetchTotal']);
            $routes->post('fetchRecouvrementSingle/(:num)', [RecouvrementController::class, 'fetchRecouvrementSingle']);
            $routes->post('create', [RecouvrementController::class, 'createRecouvrement']);
            $routes->post('delete', [RecouvrementController::class, 'removeRecouvrement']);
            $routes->post('update/(:num)', [RecouvrementController::class, 'updateRecouvrement']);
            // $routes->put('update/(:num)', 'RecouvrementController::updateRecouvrement/$1');
        });
    /**
     * route for the users mecanicien
     */
    $routes->group('/mecanicien', function ($routes) {
        $routes->get('/', [MecanicienController::class, 'index']);
        $routes->get('fetchMecanicien', [MecanicienController::class, 'fetchMecanicien']);
        // $routes->get('fetchMecanicien_1/(:num)', [MecanicienController::class, 'fetchMecanicien_1']);
        $routes->post('fetchmecanicienSingle/(:num)', [MecanicienController::class, 'fetchmecanicienSingle']);
        $routes->post('create', [MecanicienController::class, 'create']);
        $routes->post('delete', [MecanicienController::class, 'delete']);
        $routes->post('update/(:num)', [MecanicienController::class, 'update']);
        $routes->get('fetchMecanicienPerformances', [MecanicienController::class, 'fetchMecanicienPerformances']);
        
        // $routes->put('update/(:num)', 'MecanicienController::update/$1');
    });

    $routes->group('/notifications', function ($routes) {
        $routes->get('/', [NotificationController::class, 'getNotification']);
        $routes->post('markAsRead/(:num)', [NotificationController::class, 'markAsRead']);
        $routes->post('markAllAsRead', [NotificationController::class, 'markAllAsRead']);
    });
// routes for sortie caisse
$routes->group('/sortieCaisse', function ($routes) {
    $routes->get('/', [SortieCaisseController::class, 'index']);
    $routes->get('fetchSortieCaisseData', [SortieCaisseController::class, 'fetchSortieCaisseData']);
    $routes->get('fetchSortieCaisseData1', [SortieCaisseController::class, 'fetchSortieCaisseData1']);
    $routes->post('fetchSortieCaisseSingle/(:num)', [SortieCaisseController::class, 'fetchSortieCaisseSingle']);
    $routes->post('createSortieCaisse', [SortieCaisseController::class, 'createSortieCaisse']);
    $routes->post('markAsPaid/(:num)', 'SortieCaisseController::markAsPaid/$1');
    $routes->post('updateSortieCaisse/(:num)', [SortieCaisseController::class, 'updateSortieCaisse']);
    $routes->post('validateSortieCaisse/(:num)', [SortieCaisseController::class, 'validateSortieCaisse']);

    // ✅ Routes d'exportation
    $routes->get('exportExcel', [SortieCaisseController::class, 'exportExcel']);
    $routes->get('exportCsv', [SortieCaisseController::class, 'exportCsv']);
    $routes->get('exportWithFilters', [SortieCaisseController::class, 'exportWithFilters']);
});

    // remise
    $routes->group('/remise', function ($routes) {
        $routes->get('', [RemiseController::class, 'index']); 
        $routes->get('/', [RemiseController::class, 'index']);
        $routes->get('fetchRemiseData', [RemiseController::class, 'fetchRemiseData']);
        // $routes->post('delete', [RecouvrementController::class, 'removeRecouvrement']);
        $routes->post('updateRemise/(:num)', [RemiseController::class, 'updateRemise']);
    });

// avance
// ✅ DANS app/Config/Routes.php

$routes->group('avances', function ($routes) {
    // Page principale
    $routes->get('/', [AvanceController::class, 'index']);
    
    // ✅ CORRECTION : Utiliser POST pour fetchPendingValidation (cohérent avec DataTables AJAX)
    $routes->get('fetchPendingValidation', [AvanceController::class, 'fetchPendingValidation']);
    $routes->post('fetchPendingValidation', [AvanceController::class, 'fetchPendingValidation']);
    
    // GET : Récupération des données
    $routes->get('fetchAvanceData', [AvanceController::class, 'fetchAvanceData']);
    $routes->get('fetchAvanceBecameOrder', [AvanceController::class, 'fetchAvanceBecameOrder']);
    $routes->get('fetchExpiredAvance', [AvanceController::class, 'fetchExpiredAvance']);
    
    // GET : Avance spécifique
    $routes->get('fetchSingleAvance/(:num)', [AvanceController::class, 'fetchSingleAvance/$1']);
    $routes->get('getInvoicePreview/(:num)', [AvanceController::class, 'getInvoicePreview/$1']);
    $routes->get('getFullInvoiceForPrint/(:num)', [AvanceController::class, 'getFullInvoiceForPrint/$1']);
    $routes->get('printInvoice/(:num)', [AvanceController::class, 'printInvoice/$1']);
    
    // POST : Création / Modifications
    $routes->post('createAvance', [AvanceController::class, 'createAvance']);
    $routes->post('updateAvance', [AvanceController::class, 'updateAvance']);
    $routes->post('deleteAvance', [AvanceController::class, 'removeAvance']);
    $routes->post('notifyPrintInvoice', [AvanceController::class, 'notifyPrintInvoice']);
    $routes->post('processExpiredAvances', [AvanceController::class, 'processExpiredAvances']);
    
    // Paiement
    $routes->post('payAvance', [AvanceController::class, 'payAvance']);
    
    // Conversion manuelle
    $routes->get('checkAndConvertCompleted', [AvanceController::class, 'checkAndConvertCompleted']);
    $routes->post('checkAndConvertCompleted', [AvanceController::class, 'checkAndConvertCompleted']);
    
    // Forcer conversion
    $routes->get('forceConvertToOrder/(:num)', [AvanceController::class, 'forceConvertToOrder/$1']);
    
    // CRON (alertes deadline)
    $routes->get('checkDeadlineAlerts', [AvanceController::class, 'checkDeadlineAlerts']);
    
    // Validation d'avance
    $routes->get('validateAvance', [AvanceController::class, 'validateAvance']);
    $routes->post('validateAvance', [AvanceController::class, 'validateAvance']);
});

    // historique
    $routes->group('historique', ['filter' => 'auth'], static function ($routes) {
        $routes->get('/', 'HistoriqueController::index');
        $routes->get('fetchHistoriqueData', 'HistoriqueController::fetchHistoriqueData');
        $routes->get('export', 'HistoriqueController::export');
        $routes->get('stats', 'HistoriqueController::stats'); // <-- ici
        $routes->get('getStats', 'HistoriqueController::getStats'); // reste pour AJAX
        $routes->post('clean', 'HistoriqueController::clean');
    });
    
});
