<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\OnlinePlayer;
use Inertia\Inertia;
use Inertia\Response;

class OnlinePlayersController extends Controller
{
    public function __invoke(): Response
    {
        return Inertia::render('Admin/OnlinePlayers', [
            // A player who disconnected without a final heartbeat update
            // will age out of this list after 2 minutes without a fresh
            // upsert, rather than sticking around forever.
            'players' => OnlinePlayer::query()
                ->where('updated_at', '>=', now()->subMinutes(2))
                ->with('server:id,name')
                ->orderBy('name')
                ->get(),
        ]);
    }
}
