<?php

namespace App\Http\Controllers;

use App\Models\Inventory;
use App\Models\Product;
use App\Models\Sale;
use Carbon\Carbon;
use Illuminate\Http\Request;

class SaleController extends Controller
{

    public function __construct()
    {
        return $this->middleware(['auth', 'members']);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function member_save_sale(Request $request)
    {
        $pay_channel = $request->pay_channel;
        $product_id = $request->product_id;
        $amount = $request->amount;
        $date = $request->date;
        $quantity = $request->quantity;
        $product = Product::find($product_id);
        $created_by = auth()->user()->id;
        $review = $request->review;


        $sale = new Sale([
            'user_id' => $created_by,
            'category_id' => $product->category_id,
            'quantity' => $quantity,
            'amount' => $amount,
            'date' => $date,
            'created_by' => $created_by,
            'product_id' => $product_id,
            'pay_channel' => $pay_channel,
            'review' => $review,
        ]);


        $sale->save();

        $product_controller = new ProductController();
        $product = $product_controller->deplete_inventory($product_id, $quantity);

        $inventory_controller = new InventoryController();

        $depleted_inventory = $inventory_controller->deplete_inventory($product_id, $quantity, auth()->user()->role_id);

//        return redirect()->back()->with('success', "$product->name sold successfully");
        return response()->json("$product->name sold successfully");
    }

    public function member_sale_new()
    {
        $products = Product::where('category_id', auth()->user()->role_id)->get()->sortBy('name');
        $inventories = Inventory::where('category_id', auth()->user()->role_id)->get();
        return view('sales.sell', compact('products','inventories'));
    }

    public function member_todays_sales()
    {
        $sales = Sale::where('category_id',auth()->user()->role_id)
            ->whereDate('created_at', Carbon::today())->get();
        return view('sales.sales', compact('sales'));
    }

    public function member_all_sales()
    {
        $sales = Sale::all()->take(20)->sortBy('created_at');
        return view('sales.sales', compact('sales'));
    }

    public function get_sales_by_date(Request $request)
    {
        $sale_date = $request->search_date;
        $sales = Sale::where('date', $sale_date)->where('category_id',auth()->user()->role_id)->get()->sortBy('created_at');
        return view('sales.load', compact('sales'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Sale $sale
     * @return \Illuminate\Http\Response
     */
    public function show(Sale $sale)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Sale $sale
     * @return \Illuminate\Http\Response
     */
    public function edit(Sale $sale)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\Models\Sale $sale
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Sale $sale)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Sale $sale
     * @return \Illuminate\Http\Response
     */
    public function destroy(Sale $sale)
    {
        //
    }
}
