<?php

namespace App\Http\Controllers;

use App\Models\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class UsersController extends Controller
{
    /**
     * Display a listing of the resource.
     */
   public function index(Request $request)
{
    $search = $request->input('search');

    $users = Users::when($search, function ($query) use ($search) {
        $query->where('username', 'like', '%' . $search . '%')
              ->orWhere('email', 'like', '%' . $search . '%')
              ->orWhere('role', 'like', '%' . $search . '%');
    })->orderBy('user_id', 'desc')->get();

    return view('users.index', compact('users', 'search'));
}

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
         return view('users.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'username' => 'required|string|max:100|unique:tbl_users,username',
            'email'    => 'required|email|unique:tbl_users,email',
            'password' => 'required|min:6',
            'role'     => 'required|in:admin,pengguna',
        ]);

        Users::create([
            'username' => $request->username,
            'email'    => $request->email,
            'password' => Hash::make($request->password),
            'role'     => $request->role,
        ]);

        return redirect()->route('users.index')->with('success', 'User berhasil ditambahkan!');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $users = Users::findOrFail($id);
        return view('users.edit', compact('users'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        $users = Users::findOrFail($id);
        $request->validate([
            'username' => 'required|string|max:100|unique:tbl_users,username,' . $users->user_id . ',user_id',
            'email'    => 'required|email|unique:tbl_users,email,' . $users->user_id . ',user_id',
            'role'     => 'required|in:admin,pengguna',
        ]);

        $data = [
            'username' => $request->username,
            'email'    => $request->email,
            'role'     => $request->role,
        ];

        if ($request->filled('password')) {
            $data['password'] = Hash::make($request->password);
        }

        $users->update($data);

        return redirect()->route('users.index')->with('success', 'User berhasil diperbarui!');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        $users = Users::findOrFail($id);
        $users->delete();
        return redirect()->route('users.index')->with('success', 'User berhasil dihapus!');
    }
}

