<?php

namespace App\Http\Controllers;

use App\Actions\Games\CreatePlayerGameProfileAction;
use App\Actions\Games\UpdatePlayerGameProfileAction;
use App\Http\Requests\PlayerGameProfiles\StorePlayerGameProfileRequest;
use App\Http\Requests\PlayerGameProfiles\UpdatePlayerGameProfileRequest;
use App\Models\Game;
use App\Models\PlayerGameProfile;
use App\Queries\GameCatalogQuery;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class PlayerGameProfileController extends Controller
{
    public function __construct(
        private GameCatalogQuery $gameCatalog,
    ) {
        $this->authorizeResource(PlayerGameProfile::class, 'playerGameProfile');
    }

    public function index(): View
    {
        $profiles = auth()->user()
            ->playerProfile
            ->playerGameProfiles()
            ->with(['game', 'currentRank', 'primaryPlatform'])
            ->latest()
            ->get();

        return view('profile.games.index', [
            'profiles' => $profiles,
        ]);
    }

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

    public function store(
        StorePlayerGameProfileRequest $request,
        CreatePlayerGameProfileAction $action,
    ): RedirectResponse {
        $game = Game::query()->findOrFail($request->validated('game_id'));

        $profile = $action->execute(
            auth()->user()->playerProfile,
            $game,
            $request->validated(),
        );

        return redirect()
            ->route('profile.games.show', $profile)
            ->with('status', 'Game profile created.');
    }

    public function show(PlayerGameProfile $playerGameProfile): View
    {
        $playerGameProfile->load([
            'game.ranks',
            'game.roles',
            'game.characters',
            'game.platforms',
            'game.regions',
            'currentRank',
            'highestRank',
            'primaryPlatform',
            'region',
            'gameRoles.gameRole',
            'gameCharacters.gameCharacter',
            'gamePlatforms.gamePlatform',
        ]);

        return view('profile.games.show', [
            'profile' => $playerGameProfile,
        ]);
    }

    public function edit(PlayerGameProfile $playerGameProfile): View
    {
        $playerGameProfile->load([
            'game.ranks',
            'game.roles',
            'game.characters',
            'game.platforms',
            'game.regions',
            'gameRoles',
            'gameCharacters',
            'gamePlatforms',
        ]);

        return view('profile.games.edit', [
            'profile' => $playerGameProfile,
        ]);
    }

    public function update(
        UpdatePlayerGameProfileRequest $request,
        PlayerGameProfile $playerGameProfile,
        UpdatePlayerGameProfileAction $action,
    ): RedirectResponse {
        $profile = $action->execute($playerGameProfile, $request->validated());

        return redirect()
            ->route('profile.games.show', $profile)
            ->with('status', 'Game profile updated.');
    }

    public function destroy(PlayerGameProfile $playerGameProfile): RedirectResponse
    {
        $playerGameProfile->delete();

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