<?php

require_once BASE_PATH . '/app/models/ActivityLog.php';
require_once BASE_PATH . '/app/models/SystemSetting.php';

/**
 * Best-effort audit logging for active PET SPA mutations.
 * A successful business mutation must not be rolled back merely because the
 * optional audit log is unavailable.
 */
class SpaActivityLog
{
    public function record(
        int $actorId,
        string $action,
        string $entityType,
        ?int $entityId,
        array $details = []
    ): void {
        try {
            if (!(new SystemSetting())->getBoolean('activity_logging_enabled', true)) {
                return;
            }

            (new ActivityLog())->create(
                $actorId,
                $action,
                $entityType,
                $entityId,
                $details,
                $_SERVER['REMOTE_ADDR'] ?? null,
                $_SERVER['HTTP_USER_AGENT'] ?? null
            );
        } catch (Throwable) {
            // Audit logging is non-blocking; do not undo the completed action.
        }
    }
}
