<?php

namespace App\Http\Controllers;

use App\Http\Requests\MasterStandRequest;
use App\Models\MasterStand;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

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

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

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

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

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

    public function store(MasterStandRequest $request): RedirectResponse
    {
        MasterStand::create($request->validated());

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

    public function edit(MasterStand $stand): View
    {
        return view('master.stands.edit', compact('stand'));
    }

    public function update(MasterStandRequest $request, MasterStand $stand): RedirectResponse
    {
        $stand->update($request->validated());

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

    public function destroy(MasterStand $stand): RedirectResponse
    {
        $stand->delete();

        return redirect()->route('stands.index')->with('success', 'Stand berhasil dihapus (soft delete).');
    }
}
