<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Product;
use App\Models\ShopWarehouseStocks;
use App\Models\Units;
use App\Models\UsedProductMaster;
use App\Service\Shop\UsedProduct;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;

class UsedProductController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return Inertia::render('Shops/UsedProduct');
    }

    public function view()
    {
        $category = Category::get();
        $unit = Units::all();
        $product = Product::where('type', 'Row Product')->get();
        $warehouse = ShopWarehouseStocks::where('shop_id', Auth::user()->shop_id)->get();
        $usedProduct = UsedProductMaster::where('shop_id', Auth::user()->shop_id)->with('productDetails')->orderbydesc('id')->get();

        return response()->json(['category' => $category, 'unit' => $unit, 'product' => $product, 'warehouse' => $warehouse, 'usedProduct' => $usedProduct]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        UsedProduct::storeData(request()->all());
        $warehouse = ShopWarehouseStocks::where('shop_id', Auth::user()->shop_id)->get();
        $usedProduct = UsedProductMaster::where('shop_id', Auth::user()->shop_id)->with('productDetails')->orderbydesc('id')->get();

        return response(['success' => 'Data inset successfully', 'warehouse' => $warehouse, 'usedProduct' => $usedProduct]);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
