<?php
declare(strict_types=1);

// Safely records an action to the system_logs table
function systemLog(string $action, string $description): void {
    $db = new Database();
    
    // Grab the user ID if they are logged in, otherwise record it as a guest (NULL)
    $user_id = $_SESSION['user_id'] ?? null;
    
    // Safely grab the IP address
    $ip_address = $_SERVER['REMOTE_ADDR'] ?? 'UNKNOWN';

    $db->query('INSERT INTO system_logs (user_id, action, description, ip_address) VALUES (:user_id, :action, :description, :ip_address)');
    $db->bind(':user_id', $user_id);
    $db->bind(':action', $action);
    $db->bind(':description', $description);
    $db->bind(':ip_address', $ip_address);
    
    $db->execute();
}