<?php

namespace App\Support;

use App\Models\ActivityLog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class ActivityLogger
{
    public static function log(
        string $action,
        ?object $actor = null,
        ?Model $subject = null,
        array $oldValues = [],
        array $newValues = [],
        ?Request $request = null,
    ): ActivityLog {
        return ActivityLog::create([
            'actor_user_id' => $actor?->id,
            'action' => $action,
            'subject_type' => $subject ? $subject::class : null,
            'subject_id' => $subject?->getKey(),
            'old_values' => $oldValues,
            'new_values' => $newValues,
            'ip_address' => $request?->ip(),
            'user_agent' => $request?->userAgent(),
            'logged_at' => now(),
        ]);
    }
}
