<?php
/**
 * Point d'entrée principal pour les notifications
 */

require_once 'config/config.php';
require_once 'controllers/AuthController.php';
require_once 'controllers/NotificationController.php';

// Vérifier l'authentification
$authController = new AuthController();
$authController->checkAuth();

try {
    $db = new Database();
    $pdo = $db->getConnection();
    $notificationController = new NotificationController($pdo);
    
    // Récupérer l'action
    $action = $_GET['action'] ?? 'dashboard';
    
    switch ($action) {
        case 'dashboard':
            $notificationController->dashboard();
            break;
            
        case 'process_checkouts':
            $processed = $notificationController->processCheckouts();
            $_SESSION['success'] = "$processed check-outs traités automatiquement";
            header('Location: notifications.php');
            exit();
            break;
            
        case 'process_cleaning':
            $processed = $notificationController->processCleaningRequired();
            $_SESSION['success'] = "$processed notifications de nettoyage créées";
            header('Location: notifications.php');
            exit();
            break;
            
        case 'acknowledge':
            $notificationId = $_GET['id'] ?? null;
            if ($notificationId) {
                $result = $notificationController->acknowledgeNotification($notificationId, $_SESSION['user_id']);
                if ($result) {
                    $_SESSION['success'] = 'Notification acquittée';
                } else {
                    $_SESSION['error'] = 'Erreur lors de l\'acquittement';
                }
            }
            header('Location: notifications.php');
            exit();
            break;
            
        case 'complete':
            $notificationId = $_GET['id'] ?? null;
            if ($notificationId) {
                $result = $notificationController->completeNotification($notificationId, $_SESSION['user_id']);
                if ($result) {
                    $_SESSION['success'] = 'Notification terminée';
                } else {
                    $_SESSION['error'] = 'Erreur lors de la finalisation';
                }
            }
            header('Location: notifications.php');
            exit();
            break;
            
        default:
            $_SESSION['error'] = 'Action non reconnue';
            header('Location: notifications.php');
            exit();
    }
    
} catch (Exception $e) {
    $_SESSION['error'] = 'Erreur: ' . $e->getMessage();
    header('Location: dashboard.php');
    exit();
}
?>

