<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;

class PlayerController extends Controller
{
    public function index(Request $request)
    {
        $query = User::query();

        if ($request->filled('search')) {
            $query->where('username', 'ilike', '%' . $request->search . '%');
        }

        $players = $query->orderByDesc('elo')->paginate(25)->withQueryString();

        return view('admin.players.index', compact('players'));
    }

    public function ban(Request $request, int $id)
    {
        $validated = $request->validate(['reason' => 'required|string|max:500']);
        User::findOrFail($id)->update(['is_banned' => true, 'ban_reason' => $validated['reason']]);

        return back()->with('success', 'Player banned.');
    }

    public function unban(int $id)
    {
        User::findOrFail($id)->update(['is_banned' => false, 'ban_reason' => null]);

        return back()->with('success', 'Player unbanned.');
    }
}
