<?php
/**
 * Audit logging helper. Called after every state-changing admin action.
 */

require_once __DIR__ . '/../config/database.php';

function log_admin_action(string $adminEmail, string $action, string $target = '', string $details = ''): void
{
    try {
        $pdo = getDbConnection();
        $stmt = $pdo->prepare(
            'INSERT INTO audit_logs (admin_email, action, target, details, ip_address)
             VALUES (:admin_email, :action, :target, :details, :ip_address)'
        );
        $stmt->execute([
            ':admin_email' => $adminEmail,
            ':action'      => $action,
            ':target'      => $target,
            ':details'     => $details,
            ':ip_address'  => get_client_ip(),
        ]);
    } catch (Throwable $e) {
        // Audit logging must never break the main request.
        error_log('Audit log failed: ' . $e->getMessage());
    }
}
