<?php

namespace Config;

// Create a new instance of our RouteCollection class.
$routes = Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (file_exists(SYSTEMPATH . 'Config/Routes.php')) {
    require SYSTEMPATH . 'Config/Routes.php';
}

/*
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);
// The Auto Routing (Legacy) is very dangerous. It is easy to create vulnerable apps
// where controller filters or CSRF protection are bypassed.
// If you don't want to define all routes, please use the Auto Routing (Improved).
// Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.
// $routes->setAutoRoute(false);

/*
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
// $routes->get('/login', 'Auth::index', ['filter' => 'noauth']);
// $routes->post('/loginCheck', 'Auth::loginCheck');

// Filter on route group
    $routes->get('test-magento', 'Admin\TestMagento::index');
    $routes->get('dashboard', 'Admin\Dashboard::index');

    // Gestion des utilisateurs (superadmin et admin uniquement)
    $routes->group('admin/users', ['filter' => 'role:superadmin,admin'], static function ($routes) {
        $routes->get('/', 'Admin\Users::index');
        $routes->get('create', 'Admin\Users::create');
        $routes->post('store', 'Admin\Users::store');
        $routes->get('edit/(:num)', 'Admin\Users::edit/$1');
        $routes->post('update/(:num)', 'Admin\Users::update/$1');
        $routes->get('delete/(:num)', 'Admin\Users::delete/$1');
    });

    // Section Marques
    $routes->get('admin/marques', 'Admin\Marques::index');
    $routes->get('admin/marques/detail/(:any)', 'Admin\Marques::detail/$1');

    // Section Marges
    $routes->get('admin/marges', 'Admin\Marges::index');
    $routes->get('admin/marges/detail/(:num)', 'Admin\Marges::detail/$1');

    // Section Alertes Catalog
    $routes->get('admin/alertes-catalog/produits-sans-pmvc', 'Admin\AlertesCatalog::produitsSansPmvc');
    $routes->get('admin/alertes-catalog/produits-sans-code-barres', 'Admin\AlertesCatalog::produitsSansCodeBarres');
    $routes->get('admin/alertes-catalog/verif-promos', 'Admin\AlertesCatalog::verifPromos');
    $routes->get('admin/alertes-catalog/stock-promos', 'Admin\AlertesCatalog::stockPromos');
    $routes->get('admin/alertes-catalog/suggestions-destockage', 'Admin\AlertesCatalog::suggestionsDestockage');

    // Section VentesStats
    $routes->get('admin/ventesstats', 'Admin\VentesStats::index');
    $routes->get('admin/ventesstats/searchProducts', 'Admin\VentesStats::searchProducts');
    $routes->get('admin/ventesstats/searchCustomers', 'Admin\VentesStats::searchCustomers');
    $routes->get('admin/ventesstats/getCompanies', 'Admin\VentesStats::getCompanies');
    $routes->get('admin/ventesstats/getStats', 'Admin\VentesStats::getStats');
    $routes->get('admin/ventesstats/getCustomers', 'Admin\VentesStats::getCustomers');
    $routes->get('admin/ventesstats/getChildren', 'Admin\VentesStats::getChildren');
    $routes->get('admin/ventesstats/getTrend', 'Admin\VentesStats::getTrend');

    // Section Ventes Filtres
    $routes->get('admin/ventesfiltres', 'Admin\VentesFiltres::index');
    $routes->get('admin/ventesfiltres/searchCriteria', 'Admin\VentesFiltres::searchCriteria');
    $routes->get('admin/ventesfiltres/getCompanies', 'Admin\VentesFiltres::getCompanies');
    $routes->get('admin/ventesfiltres/getResults', 'Admin\VentesFiltres::getResults');

    // Section Analyse Ventes
    $routes->get('admin/analyseventes', 'Admin\AnalyseVentes::index');
    $routes->get('admin/analyseventes/searchCriteria', 'Admin\AnalyseVentes::searchCriteria');
    $routes->get('admin/analyseventes/getAnalysis', 'Admin\AnalyseVentes::getAnalysis');
    $routes->post('admin/analyseventes/getAiInsights', 'Admin\AnalyseVentes::getAiInsights');

    // Section Ventes Annuelles
    $routes->get('admin/ventesannuelles', 'Admin\VentesAnnuelles::index');

    // Section Projets Perfex
    $routes->get('admin/projets/promos', 'Admin\ProjetsPerfex::promos');
    $routes->get('admin/projets/lancements', 'Admin\ProjetsPerfex::lancements');

    // Gestion des exclusions
    $routes->group('admin/exclusions', ['filter' => 'role:superadmin,admin'], static function ($routes) {
        $routes->get('/', 'Admin\Exclusions::index');
        $routes->post('add', 'Admin\Exclusions::add');
        $routes->get('delete/(:num)', 'Admin\Exclusions::delete/$1');
    });

    // Gestion des rôles et permissions (superadmin et admin uniquement)
    $routes->group('admin/roles', ['filter' => 'role:superadmin,admin'], static function ($routes) {
        $routes->get('/', 'Admin\Roles::index');
        $routes->get('create', 'Admin\Roles::create');
        $routes->post('store', 'Admin\Roles::store');
        $routes->get('edit/(:num)', 'Admin\Roles::edit/$1');
        $routes->post('update/(:num)', 'Admin\Roles::update/$1');
        $routes->get('delete/(:num)', 'Admin\Roles::delete/$1');

        $routes->get('permissions', 'Admin\Roles::permissions');
        $routes->post('permissions/store', 'Admin\Roles::storePermission');
        $routes->get('permissions/delete/(:num)', 'Admin\Roles::deletePermission/$1');
    });

    // Routes d'authentification Shield
    service('auth')->routes($routes);

    // Surcharge de la vue de login pour utiliser le template Adminto
    $routes->get('login', 'Home::root/auth-login');
    $routes->get('register', 'Home::root/auth-register');
    $routes->get('forgot-password', 'Home::root/auth-recoverpw');

    $routes->get('profile', 'Home::root/apps-user-profile');

    $routes->get('/', 'Home::index');
    $routes->get('/(:any)', 'Home::root/$1');

/*
 * --------------------------------------------------------------------
 * Additional Routing
 * --------------------------------------------------------------------
 *
 * There will often be times that you need additional routing and you
 * need it to be able to override any defaults in this file. Environment
 * based routes is one such time. require() additional route files here
 * to make that happen.
 *
 * You will have access to the $routes object within that file without
 * needing to reload it.
 */
if (is_file(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) {
    require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
}
