<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\FiveMServer;
use App\Models\ServerHeartbeat;
use Inertia\Inertia;
use Inertia\Response;

class ServerStatusController extends Controller
{
    public function __invoke(): Response
    {
        $servers = FiveMServer::query()->get()->map(function (FiveMServer $server) {
            $latest = ServerHeartbeat::query()
                ->where('fivem_server_id', $server->id)
                ->latest('recorded_at')
                ->first();

            return [
                'id' => $server->id,
                'name' => $server->name,
                'is_active' => $server->is_active,
                'online' => $latest && $latest->recorded_at->gt(now()->subMinutes(5)),
                'player_count' => $latest?->player_count ?? 0,
                'max_players' => $latest?->max_players ?? 0,
                'resources' => $latest?->resources ?? [],
                'recorded_at' => $latest?->recorded_at,
            ];
        });

        return Inertia::render('Admin/Dashboard', ['servers' => $servers]);
    }
}
