<?php

namespace App\Http\Controllers;

use App\Http\Requests\MasterWaiterRequest;
use App\Models\MasterWaiter;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class MasterWaiterController extends Controller
{
    public function index(Request $request): View
    {
        $query = MasterWaiter::query();
        $search = $request->query('q');

        if ($search) {
            $query->where('nama', 'like', '%' . $search . '%');
        }

        $waiters = $query->orderByDesc('id')->paginate(10)->withQueryString();

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

    public function create(): View
    {
        return view('master.waiters.create');
    }

    public function store(MasterWaiterRequest $request): RedirectResponse
    {
        MasterWaiter::create($request->validated());

        return redirect()->route('waiters.index')->with('success', 'Waiter berhasil ditambahkan.');
    }

    public function edit(MasterWaiter $waiter): View
    {
        return view('master.waiters.edit', compact('waiter'));
    }

    public function update(MasterWaiterRequest $request, MasterWaiter $waiter): RedirectResponse
    {
        $waiter->update($request->validated());

        return redirect()->route('waiters.index')->with('success', 'Waiter berhasil diperbarui.');
    }

    public function destroy(MasterWaiter $waiter): RedirectResponse
    {
        $waiter->delete();

        return redirect()->route('waiters.index')->with('success', 'Waiter berhasil dihapus.');
    }
}
