prepare("INSERT INTO audit_logs (tenant_id, user_id, action, entity_type, entity_id, old_data, new_data, ip_address, user_agent, metadata, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())"); $stmt->execute([ $tenantId, $userId, $action, $entityType, $entityId, $oldData ? json_encode($oldData, JSON_UNESCAPED_UNICODE) : null, $newData ? json_encode($newData, JSON_UNESCAPED_UNICODE) : null, $_SERVER['REMOTE_ADDR'] ?? null, $_SERVER['HTTP_USER_AGENT'] ?? null, $metadata ? json_encode($metadata, JSON_UNESCAPED_UNICODE) : null, ]); } catch (\Throwable $e) { error_log('[Audit] Failed: ' . $e->getMessage()); } } }