<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Services\UserStatsService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

/**
 * Recomputes user_statistics for a batch of user IDs.
 * Dispatched by RecomputeUserStatsCommand when --queue is set.
 */
class RecomputeUserStatsJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public int $tries   = 2;
    public int $timeout = 300;

    /**
     * @param  array<int>  $userIds
     */
    public function __construct(public readonly array $userIds) {}

    public function handle(UserStatsService $statsService): void
    {
        foreach ($this->userIds as $userId) {
            $statsService->recomputeUser((int) $userId);
        }
    }
}
