<?php
/**
 * Store Keeper Auth Check
 * Verify if user is logged in, if not redirect to login
 */

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

// Check if store keeper is logged in
if (!isset($_SESSION['store_keeper_id']) || empty($_SESSION['store_keeper_id'])) {
    // Redirect to login page
    header('Location: index.php');
    exit;
}

// Session timeout check
$timeout = 1800; // 30 minutes
$_SESSION['last_activity'] = $_SESSION['last_activity'] ?? time();

if (time() - $_SESSION['last_activity'] > $timeout) {
    session_destroy();
    header('Location: index.php?session=expired');
    exit;
}

// Update last activity
$_SESSION['last_activity'] = time();

// Make user info available globally
$store_keeper_id = $_SESSION['store_keeper_id'];
$store_keeper_name = $_SESSION['store_keeper_name'] ?? 'Store Keeper';
$store_keeper_email = $_SESSION['store_keeper_email'] ?? '';
$store_keeper_phone = $_SESSION['store_keeper_phone'] ?? '';
?>