<?php

namespace App\Http\Controllers;

use App\Models\Event;
use App\Models\Game;
use App\Queries\EventQuery;
use App\ViewModels\EventPresenter;
use Illuminate\View\View;

class EventController extends Controller
{
    public function __construct(
        private EventQuery $eventQuery,
        private EventPresenter $presenter,
    ) {}

    public function index(): View
    {
        $events = $this->eventQuery->publicListing(request()->only(['game', 'status', 'pricing', 'q']));
        $featured = $this->presenter->mapCollection($this->eventQuery->featured());

        return view('pages.tournaments.index', [
            'tournaments' => $this->presenter->mapCollection($events->items()),
            'featured' => $featured,
            'pagination' => $events,
            'games' => Game::query()->where('status', 'active')->orderBy('name')->get(),
        ]);
    }

    public function show(Event $event): View
    {
        $this->authorize('view', $event);

        $event->load(['game', 'creator', 'prizes', 'eventRules', 'paymentMethods.manualPaymentMethod']);

        return view('pages.tournaments.show', [
            'tournament' => $this->presenter->toTournamentArray($event),
            'event' => $event,
            'teams' => [],
        ]);
    }
}
