<?php

namespace App\Services;

use App\Models\MexcoAuditLog;
use Illuminate\Http\Request;

class AuditService
{
    public function log(
        string $action,
        ?string $entityType = null,
        ?string $entityId = null,
        ?array $oldValues = null,
        ?array $newValues = null,
        ?Request $request = null,
    ): MexcoAuditLog {
        $req = $request ?? request();

        return MexcoAuditLog::query()->create([
            'admin_user_id' => session('meetup_super_admin_id'),
            'actor_type' => session('meetup_super_admin_id') ? 'super_admin' : null,
            'actor_id' => session('meetup_super_admin_id'),
            'action' => $action,
            'entity_type' => $entityType,
            'entity_id' => $entityId,
            'old_values' => $oldValues,
            'new_values' => $newValues,
            'ip_address' => $req?->ip(),
            'user_agent' => $req?->userAgent(),
            'created_at' => now(),
        ]);
    }
}
