<?php
/*************************************************
 * BOOTSTRAP GLOBAL
 *************************************************/
session_start();

require_once __DIR__ . '/init.php';
require_once __DIR__ . '/includes/saas_guard.php';
require_once __DIR__ . '/includes/check_auth.php';
//require_once __DIR__ . '/includes/secure_query.php';


/*************************************************
 * ROUTING
 *************************************************/
$page = $_GET['page'] ?? 'dashboard';

/*************************************************
 * PERMISSIONS GLOBALES
 *************************************************/
check_page_permission($page);

/*************************************************
 * LISTE DES PAGES AUTORISÉES
 *************************************************/
$allowedPages = [

    // CORE
    'dashboard'        => 'pages/dashboard.php',
    'profil'           => 'pages/profil/profile.php',

    // POS
    'pos-entry'        => 'pages/pos/pos-entry.php',
    'pos-select-caisse'=> 'pages/pos/pos-select-caisse.php',
    'pos-pin'          => 'pages/pos/pos-pin.php',
    'pos-open'         => 'pages/pos/pos-open.php',
    'pos'              => 'pages/pos/pos.php',
    'pos-liste'        => 'pages/pos/pos-liste.php',
    'fermer_caisse'    => 'pages/pos/fermer_caisse.php',

    //Ventes
    'ventes'           => 'pages/ventes/ventes.php',

    //devis
    'devis'           => 'pages/devis/devis.php',
    'commandes_clients'  => 'pages/devis/commandes_clients.php',
    'livraisons'           => 'pages/livraisons/livraisons.php',
    'factures'           => 'pages/factures/factures.php',
    'facture_add'           => 'pages/factures/facture_add.php',
    'encaissements_clients'  => 'pages/factures/encaissements.php',

    // PRODUCTIONS
    'productions'      => 'pages/productions/productions.php',
    'matiere-premiere' => 'pages/productions/matiere-premiere.php',
    'produits-finis'   => 'pages/productions/produits-finis.php',
    'fiches-techniques'=> 'pages/productions/fiches-techniques.php',
    'simulation'       => 'pages/productions/simulation.php',

    // ACHATS
    'achats'           => 'pages/achats/achats.php',
    'factures_fournisseurs'           => 'pages/achats/factures_fournisseurs.php',
    'appros'           => 'pages/appro/appros.php',
    'commandes_fournisseurs' => 'pages/achats/commandes_fournisseurs.php',
    'commande_fournisseur_add' => 'pages/achats/commande_fournisseur_add.php',
    'commande_fournisseur_view'=> 'pages/achats/commande_fournisseur_view.php',
    'reception_fournisseur_view'=> 'pages/achats/reception_fournisseur_view.php',
    'reception_fournisseur'    => 'pages/achats/reception_fournisseur.php',
    'receptions_fournisseurs'  => 'pages/achats/receptions_fournisseurs.php',
    'reception_fournisseur_add' => 'pages/achats/reception_fournisseur_add.php',
    'reception_liste'          => 'pages/achats/reception_liste.php',
    'valider_reception_modal'          => 'pages/achats/valider_reception_modal.php',
    'paiements_fournisseurs'          => 'pages/achats/paiements_fournisseurs.php',
    'ecarts_commande_reception'=> 'pages/achats/ecarts_commande_reception.php',

    //STOCKS
    'produits'         => 'pages/produits/produits.php',
    'categories'       => 'pages/categories/categories.php',
    'retours'          => 'pages/retours/retours.php',
    'mouvements'       => 'pages/stocks/mouvements.php',
    'inventaire'       => 'pages/stocks/inventaire.php',
    'unite'            => 'pages/unites/unites.php',
    'depots'           => 'pages/depots/depots.php',

    // CLIENTS / CRM
    'clients'          => 'pages/clients/clients.php',
    'fournisseurs'     => 'pages/fournisseurs/fournisseurs.php',
    'crm_clients'      => 'pages/crm/clients_liste.php',
    'crm_client'       => 'pages/crm/client_view.php',
    'crm_interactions' => 'pages/crm/interactions_liste.php',
    'crm_add_interaction' => 'pages/crm/interaction_add.php',
    'crm_opportunites' => 'pages/crm/opportunites.php',
    'crm_dashboard'    => 'pages/crm/dashboard.php',

    // FINANCE
    'tresorerie'       => 'pages/tresorerie/tresorerie.php',
    'caisse_especes'   => 'pages/caisses/caisse.php',
    'caisse_banque'    => 'pages/caisses/caisse.php',
    'caisse_mobile'    => 'pages/caisses/caisse.php',
    'autres_operations'=> 'pages/tresorerie/autres_operations.php',
    'creances'         => 'pages/creances/creances.php',
    'dettes'           => 'pages/dettes/dettes.php',

    // PARAMÈTRES
    'parametres'       => 'pages/parametres/parametres.php',
    'caisses'          => 'pages/parametres/caisses/caisses.php',
    'caisse_types'     => 'pages/parametres/caisses/caisse_types.php',
    'affectation_utilisateurs' => 'pages/parametres/caisses/affectation_utilisateurs.php',
    'utilisateurs'     => 'pages/utilisateurs/utilisateurs.php',
    'unites'          => 'pages/parametres/unites/unites.php',
    'roles'            => 'pages/roles/roles.php',

    // RAPPORTS
    'rapports_achats'  => 'pages/rapports/rapports_achats.php',
    'rapports_ventes'  => 'pages/rapports/rapports_ventes.php',
    'rapports_caisse'  => 'pages/rapports/rapports_caisse.php',
    'rapports_stocks'  => 'pages/rapports/rapports_stocks.php',
    'export_report_achats' => 'pages/rapports/export_report_achats.php',
    'export_report_ventes' => 'pages/rapports/export_report_ventes.php',
    'export_report_caisse' => 'pages/rapports/export_report_caisse.php',

    // ERREUR
    '404' => 'pages/404.php'
];

$contentFile = $allowedPages[$page] ?? $allowedPages['404'];

/* $posLayoutPages = [
    'pos'
]; */

$posAuthPages = [
    'pos-entry',
    'pos-select-caisse',
    'pos-pin',
    'pos-open'
];


/*************************************************
 * PAGES SANS LAYOUT
 *************************************************/
$noLayoutPages = [
    'pos-entry',
    'pos-select-caisse',
    'pos-pin',
    'pos-open',
    'export_report_achats',
    'export_report_ventes',
    'export_report_caisse'
];

/*************************************************
 * PAGES POS PROTÉGÉES PAR GUARD
 *************************************************/
$posProtectedPages = [
    'pos',
    'pos-liste',
    'fermer_caisse'
];

/*************************************************
 * APPLICATION DU POS GUARD (AVANT HTML)
 *************************************************/
if (in_array($page, $posProtectedPages)) {
    require __DIR__ . "/pages/pos/pos_guard.php";
}

/*************************************************
 * RENDU
 *************************************************/
if (in_array($page, $noLayoutPages)) {
    require __DIR__ . "/" . $contentFile;
    exit;
}

if (in_array($page, $posAuthPages)) {
    require __DIR__ . "/includes/layout_pos_auth.php";
    require __DIR__ . "/" . $contentFile;
    exit;
}

/* if (in_array($page, $posLayoutPages)) {
    require __DIR__ . "/includes/layout_pos.php";
    require __DIR__ . "/" . $contentFile;
    exit;
} */

require __DIR__ . "/includes/layout.php";
require __DIR__ . "/" . $contentFile;
