<?php

namespace App\Jobs;

use App\Models\User;
use App\Services\DiscordRoleSyncService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

/**
 * Hourly catch-up sync (see routes/console.php) for accounts whose Discord
 * roles changed without the player logging back into the UCP — the
 * per-login SyncDiscordRoleForUser job only fires at login time.
 */
class SyncDiscordRoles implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function handle(DiscordRoleSyncService $service): void
    {
        User::query()->whereNotNull('discord_id')->each(fn (User $user) => $service->syncForUser($user));
    }
}
