<?php

function log_admin_activity($action, $description = '')
{
    global $pdo;

    $adminId = null;

    if (function_exists('admin')) {
        $currentAdmin = admin();
        $adminId = $currentAdmin['id'] ?? null;
    }

    $ipAddress = $_SERVER['REMOTE_ADDR'] ?? null;
    $userAgent = $_SERVER['HTTP_USER_AGENT'] ?? null;

    try {
        $stmt = $pdo->prepare("
            INSERT INTO admin_activity_logs (
                admin_id,
                action,
                description,
                ip_address,
                user_agent
            ) VALUES (?, ?, ?, ?, ?)
        ");

        $stmt->execute([
            $adminId,
            $action,
            $description !== '' ? $description : null,
            $ipAddress,
            $userAgent
        ]);

        return true;
    } catch (Exception $e) {
        return false;
    }
}