<?php
// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

function isLoggedIn() {
    return isset($_SESSION['user_id']);
}

function requireLogin() {
    if (!isLoggedIn()) {
        header('Location: index.php');
        exit;
    }
}

function getUserId() {
    return $_SESSION['user_id'] ?? null;
}

function getUserCompany($pdo, $userId) {
    $stmt = $pdo->prepare("SELECT * FROM companies WHERE user_id = ? LIMIT 1");
    $stmt->execute([$userId]);
    return $stmt->fetch();
}
?>