<?php
// customer/logout.php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Clear remember-me cookie if used
setcookie('remember_token', '', time() - 3600, '/');

// Clear session data
$_SESSION = [];

if (ini_get('session.use_cookies')) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params['path'],
        $params['domain'],
        $params['secure'],
        $params['httponly']
    );
}

session_destroy();

// Redirect back to the main unified login.php, even when the project is inside /ushs/
$customerDir = rtrim(str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'] ?? '')), '/');
$appBase = rtrim(dirname($customerDir), '/');
if ($appBase === '/' || $appBase === '\\' || $appBase === '.') {
    $appBase = '';
}

header('Location: ' . $appBase . '/login.php');
exit();
