<?php
namespace App\Http\Controllers\Api\Admin;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index(Request $request)
    {
        $users = User::when($request->search,fn($q)=>$q->where('name','like',"%{$request->search}%")->orWhere('email','like',"%{$request->search}%"))->latest()->paginate(20);
        return response()->json(['success'=>true,'data'=>$users]);
    }
    public function updateRole(Request $request, int $id)
    {
        $request->validate(['role'=>'required|in:admin,customer']);
        User::findOrFail($id)->update(['role'=>$request->role]);
        return response()->json(['success'=>true,'message'=>'Role updated.']);
    }
}
