<?php

namespace App\Http\Controllers\Admin;

use App\Enums\UserRole;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\View\View;

class UserController extends Controller
{
    /**
     * Display Users Page.
     */
    public function index(): View
    {
        return view('admin.users.index');
    }

    /**
     * Update user (ban/unban).
     */
    public function update(Request $request)
    {
        $validated = $request->validate([
            'id' => 'required|exists:users,id',
            'action' => 'required',
        ]);

        $user = User::findOrFail($validated['id']);

        switch ($validated['action']) {
            case 'ban':
                $user->addRole(UserRole::BANNED);
                alert()->success('Banned', 'User has been banned.');
                break;
            case 'unban':
                $user->removeRole(UserRole::BANNED);
                alert()->success('Unbanned', 'User has been unbanned.');
                break;
            default:
                alert()->error('Error', 'Invalid action provided');
        }

        return redirect()->back();
    }
}
