<?php
namespace App\Http\Controllers\Admin\Inventory;
use App\Http\Controllers\Controller;
use App\Models\Inventory\Warehouse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class WarehouseController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = Warehouse::query()->with('manager');
        $query->when($request->name, fn($q, $v) => $q->where('name', 'like', "%{$v}%"));
        return $this->paginate($query->latest()->paginate($request->input('per_page', 15)));
    }
    public function store(Request $request): JsonResponse
    {
        $v = $request->validate(['name'=>'required|string|max:50','manager_id'=>'nullable|exists:users,id','status'=>'sometimes|integer|in:0,1']);
        return $this->success(Warehouse::create($v));
    }
    public function show(Warehouse $warehouse): JsonResponse { return $this->success($warehouse->load('manager')); }
    public function update(Request $request, Warehouse $warehouse): JsonResponse { $v = $request->validate(['name'=>'sometimes|string|max:50','manager_id'=>'nullable|exists:users,id','status'=>'sometimes|integer|in:0,1']); $warehouse->update($v); return $this->success($warehouse); }
    public function destroy(Warehouse $warehouse): JsonResponse { $warehouse->delete(); return $this->success(null); }
}
