<?php
namespace Core\Middlewares;

class AuthMiddleware {
    public function handle() {
        if (!isset($_SESSION['user_id'])) {
            if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
                http_response_code(401);
                echo json_encode(['success' => false, 'redirect' => BASE_URL . '/auth/login', 'message' => 'انتهت الجلسة. يرجى تسجيل الدخول']);
                exit;
            }
            header("Location: " . BASE_URL . "/auth/login");
            exit;
        }
    }
}
