<?php
// ============================================================
//  Turi Express Courier Services — Auth Helper
//  File: includes/auth.php
// ============================================================

require_once __DIR__ . '/config.php';

if (session_status() === PHP_SESSION_NONE) {
    session_name(SESSION_NAME);
    session_start();
}

/**
 * Returns true if an admin/staff is currently logged in.
 */
function is_logged_in(): bool {
    return isset($_SESSION['admin_id']) && !empty($_SESSION['admin_id']);
}

/**
 * Redirect to admin login page if not authenticated.
 */
function require_login(): void {
    if (!is_logged_in()) {
        header('Location: signin.php');
        exit;
    }
}

/**
 * Log the current admin out and destroy the session.
 */
function logout(): void {
    $_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();
    header('Location: signin.php');
    exit;
}
