<?php

namespace App\Services;

use App\Models\AuditLog;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;

class AuditLogService
{
    public function log(
        ?User $user,
        string $action,
        ?Model $model = null,
        array $oldValues = [],
        array $newValues = [],
        ?string $deviceId = null
    ): void {
        AuditLog::create([
            'company_id' => $user?->company_id ?? $model?->getAttribute('company_id'),
            'user_id' => $user?->id,
            'action' => $action,
            'auditable_type' => $model ? $model::class : null,
            'auditable_id' => $model?->getKey(),
            'old_values' => $oldValues ?: null,
            'new_values' => $newValues ?: null,
            'ip_address' => request()->ip(),
            'user_agent' => request()->userAgent(),
            'device_id' => $deviceId,
        ]);
    }
}
