<?php

namespace App\Http\Controllers;

use App\Models\PlayerGameProfile;
use App\Queries\GameCatalogQuery;
use Illuminate\View\View;

class GameController extends Controller
{
    public function __construct(
        private GameCatalogQuery $gameCatalog,
    ) {}

    public function index(): View
    {
        return view('pages.games.index', [
            'games' => $this->gameCatalog->allActive(),
        ]);
    }

    public function show(string $game): View
    {
        $gameModel = $this->gameCatalog->findActiveBySlugOrFail($game);

        $players = PlayerGameProfile::query()
            ->where('game_id', $gameModel->id)
            ->where('is_public', true)
            ->with(['playerProfile.user', 'currentRank'])
            ->latest()
            ->limit(10)
            ->get();

        return view('pages.games.show', [
            'game' => $gameModel,
            'players' => $players,
        ]);
    }
}
