<?php

namespace App\Services;

use App\Models\Auth\User;
use Illuminate\Support\Facades\DB;

class UserPrivilegeService
{
    public function privilegesChanged(User $user, ?callable $mutator = null): User
    {
        return DB::transaction(function () use ($user, $mutator) {
            if ($mutator) {
                $mutator($user);
            }

            $user->increment('privileges_version');
            $user->refresh();

            app(JwtService::class)->forceRefresh($user);

            return $user;
        });
    }
}