<?php
// app/Http/Controllers/StokController.php
namespace App\Http\Controllers;

use App\Models\StokAkhir;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class StokAkhirController extends Controller
{
    /**
     * Menampilkan semua data stok
     */
    public function index()
    {
        $stok = StokAkhir::all();
        return view('admin.stok', compact('stok'));
    }

    /**
     * Menyimpan data stok baru
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id_stok'      => 'required|string|max:50|unique:stok_akhir,id_stok',
            'jenis_barang' => 'required|string|max:50|unique:stok_akhir,jenis_barang',
            'stok_isi'     => 'required|integer|min:0',
            'stok_kosong'  => 'required|integer|min:0',
        ]);

        if ($validator->fails()) {
            return response()->json($validator->errors(), 400);
        }

        // Kalkulasi total aset secara otomatis
        $total_aset = $request->stok_isi + $request->stok_kosong;

        $stok = StokAkhir::create([
            'id_stok'      => $request->id_stok,
            'jenis_barang' => $request->jenis_barang,
            'stok_isi'     => $request->stok_isi,
            'stok_kosong'  => $request->stok_kosong,
            'total_aset'   => $total_aset,
        ]);

        return response()->json([
            'message' => 'Data stok berhasil ditambahkan',
            'data'    => $stok
        ], 201);
    }

    /**
     * Update data berdasarkan id_stok
     */
/**
 * Update data berdasarkan id_produk
 * Logika: admin dapat mengubah stok_isi secara manual.
 * stok_kosong dihitung otomatis: total_aset - stok_isi_baru
 * Sehingga jika stok_isi habis terjual (turun), stok_kosong otomatis bertambah.
 */
public function update(Request $request, $id_produk)
{
    $stok = StokAkhir::findOrFail($id_produk);

    // Validasi input dasar
    $request->validate([
        'total_aset' => 'required|integer|min:0',
        'stok_isi'   => 'required|integer|min:0',
    ]);

    $totalAsetBaru = (int) $request->total_aset;
    $stokIsiBaru   = (int) $request->stok_isi;

    // Validasi manual: stok isi tidak boleh melebihi total aset
    if ($stokIsiBaru > $totalAsetBaru) {
        return redirect()->back()
            ->withErrors(['stok_isi' => 'Stok Isi tidak boleh melebihi Total Aset (' . $totalAsetBaru . ').'])
            ->withInput();
    }

    // Hitung stok_kosong otomatis
    $stokKosongBaru = max(0, $totalAsetBaru - $stokIsiBaru);

    // Simpan perubahan
    $stok->update([
        'total_aset'  => $totalAsetBaru,
        'stok_isi'    => $stokIsiBaru,
        'stok_kosong' => $stokKosongBaru,
    ]);

    return redirect()->route('admin.stok')->with('success',
        'Stok diperbarui! Total Aset: ' . number_format($totalAsetBaru) .
        ' | Stok Isi: ' . number_format($stokIsiBaru) .
        ' | Stok Kosong: ' . number_format($stokKosongBaru)
    );
}

    /**
     * Menghapus data
     */
    public function destroy($id_stok)
    {
        $stok = StokAkhir::find($id_stok);

        if (!$stok) {
            return response()->json(['message' => 'Data tidak ditemukan'], 404);
        }

        $stok->delete();
        return response()->json(['message' => 'Data berhasil dihapus']);
    }
}