<?php

namespace App\Http\Controllers\Admin;

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

class UserController extends Controller
{
    /**
     * Display Users Page.
     */
    public function index(): \Illuminate\View\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->update(['is_banned' => 1]);
                alert()->success('Banned', 'User has been banned.');
                break;
            case 'unban':
                $user->update(['is_banned' => 0]);
                alert()->success('Unbanned', 'User has been unbanned.');
                break;
            default:
                alert()->error('Error','Invalid action provided');
        }

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