<?php

namespace App\Http\Controllers;

use App\Actions\Videos\CreatePlayerVideoAction;
use App\Http\Requests\Videos\StorePlayerVideoRequest;
use App\Models\PlayerVideo;
use App\Queries\GameCatalogQuery;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

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

    public function index(): View
    {
        $this->authorize('viewAny', PlayerVideo::class);

        $profile = auth()->user()->playerProfile;

        $videos = $profile
            ? $profile->videos()
                ->with(['game'])
                ->latest()
                ->paginate(12)
            : PlayerVideo::query()->whereRaw('1 = 0')->paginate(12);

        return view('pages.profile.videos.index', [
            'videos' => $videos,
            'games' => $this->gameCatalog->allActive(),
            'gameProfiles' => $profile?->gameProfiles()->with('game')->get() ?? collect(),
        ]);
    }

    public function store(
        StorePlayerVideoRequest $request,
        CreatePlayerVideoAction $action,
    ): RedirectResponse {
        $action->execute(auth()->user(), $request->validated());

        return redirect()
            ->route('profile.videos.index')
            ->with('status', __('admin.moderation.videos').' saved.');
    }

    public function destroy(PlayerVideo $video): RedirectResponse
    {
        $this->authorize('delete', $video);
        $video->delete();

        return redirect()
            ->route('profile.videos.index')
            ->with('status', 'Video deleted.');
    }
}
