<?php
namespace App\Helpers;

use App\Models\WorkflowAudit;
use Illuminate\Support\Facades\Auth;

class AuditLogger
{
    public static function log($entityType, $entityId, $action, $before = null, $after = null)
    {
        try {
            WorkflowAudit::create([
                'entity_type' => $entityType,
                'entity_id' => $entityId,
                'user_id' => Auth::id(),
                'action' => $action,
                'before' => $before,
                'after' => $after,
            ]);
        } catch (\Throwable $e) {
            \Log::warning('Failed to write audit log entry', [
                'entity_type' => $entityType,
                'entity_id' => $entityId,
                'action' => $action,
                'error' => $e->getMessage(),
            ]);
        }
    }
}
