<?php

namespace App\Http\Controllers\Admin\Warehouse;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\ShopWarehouseStocks;
use App\Models\Units;
use App\Service\Warehouse\WarehouseShopService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;

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

    public function view()
    {

        return response()->json(['warehouse' => ShopWarehouseStocks::orderByDesc('id')
            ->where('shop_id', auth()->user()->shop_id)
            ->with(['product', 'warehouse', 'getAttr'])
            ->groupBy('product_id', 'shop_id')
            ->select(
                'product_id',
                'shop_id',
                DB::raw('SUM(purchase_total) as purchase_total'),
                DB::raw('SUM(quantity) as quantity')
            )->get(),  'unit' => Units::get(), 'category' => Category::whereNull('parent_id')->with('child')->get(), 'opening' => ShopWarehouseStocks::where('shop_id', auth()->user()->shop_id)->whereNotNull('opening_stock_date')->first()]);
    }

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

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request) {}

    /**
     * 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)
    {
        WarehouseShopService::UpdateDataStore(request()->all());

        return response()->json(['success' => 'Data insert successfully', 'warehouse' => ShopWarehouseStocks::orderByDesc('id')
            ->where('shop_id', auth()->user()->shop_id)
            ->with(['product', 'warehouse', 'getAttr'])
            ->groupBy('product_id', 'shop_id')
            ->select(
                'product_id',
                'shop_id',
                DB::raw('SUM(purchase_total) as purchase_total'),
                DB::raw('SUM(quantity) as quantity')
            )->get(), 'opening' => ShopWarehouseStocks::where('shop_id', auth()->user()->shop_id)->whereNotNull('opening_stock_date')->first()]);
    }

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