<?php
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Item;
use Illuminate\Http\Request;

class ItemController extends Controller
{
    public function store(Request $request)
    {
        // 1. Validasi data yang dikirim wajib ada dan sesuai format
        $validated = $request->validate([
            'store_id' => 'required|exists:stores,id',
            'name'     => 'required|string|max:255',
            'type'     => 'required|in:bahan,sub_bahan,produk',
            'uom'      => 'required|string|max:50',
        ]);

        // 2. Simpan ke database
        $item = Item::create($validated);

        // 3. Kembalikan response JSON sukses
        return response()->json([
            'status'  => 'success',
            'message' => 'Item berhasil ditambahkan',
            'data'    => $item
        ], 201);
    }
    public function show($id)
    {
        // Cari item berdasarkan ID, lalu tarik juga relasi resepnya (ingredients)
        $item = Item::with('ingredients')->findOrFail($id);

        return response()->json([
            'status'  => 'success',
            'data'    => $item
        ], 200);
    }
    
}