<?php

function logActivity(int $userId, string $action, string $details): void
{
    $pdo = getDbConnection();
    
    $username = $_SESSION['username'] ?? 'system';
    
    $stmt = $pdo->prepare("INSERT INTO activity_log (user_id, username, action, details, timestamp) VALUES (?, ?, ?, ?, NOW())");
    $stmt->execute([$userId, $username, $action, $details]);
}

function getActivityLog(?int $limit = 100): array
{
    $pdo = getDbConnection();
    
    $stmt = $pdo->prepare("SELECT * FROM activity_log ORDER BY timestamp DESC LIMIT ?");
    $stmt->execute([$limit]);
    
    return $stmt->fetchAll();
}

function getUserActivity(int $userId, ?int $limit = 50): array
{
    $pdo = getDbConnection();
    
    $stmt = $pdo->prepare("SELECT * FROM activity_log WHERE user_id = ? ORDER BY timestamp DESC LIMIT ?");
    $stmt->execute([$userId, $limit]);
    
    return $stmt->fetchAll();
}
