<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Jobs\RunGameJob;
use App\Models\Game;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;

class GameJobController extends Controller
{
    public function index(Request $request)
    {
        if (RateLimiter::tooManyAttempts('start-game', 1)) {
            return back()->with('error', 'Next start game request avaible in 1 minute.');
        }
        RateLimiter::increment('start-game');

        $checkCurrentRunnigGame = Game::currentRunning()->exists();
        if ($checkCurrentRunnigGame) {
            return back()->with('error', 'Game is already running.');
        }
        dispatch(new RunGameJob)->onQueue('default');
        return redirect()->route('dashboard')->with('success', 'Game start successfully.');
    }
}
