<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Repositories\ResponseRepository;
use Illuminate\Support\Facades\Log;
use App\Models\Product;
use App\Models\ProductInStock;
use App\Models\ProductOutStock;

class ProductInStockController extends Controller
{
    public function __construct(ResponseRepository $response) {
        $this->response = $response;
        $this->storeProductManual = [
            'product_id' => 'required',
            'product_code' => 'required',
            'category_id' => 'required',
            'brand_id' => 'required',
            'color_id' => 'required',
            'size_id' => 'required',
            'style_id' => 'required',
            'product_manual_quantity' => 'required',
            'product_price' => 'required'
        ];
    }

    public function index() {
        return $this->response->jsonResponse(false, "Product In Stock Details Fetched Successfully", ProductInStock::with('category', 'brand', 'color', 'size', 'style')->orderBy('product_id', 'desc')->get(), 900);
    }

    public function getProductOutStock() {
        return $this->response->jsonResponse(false, "Product Out Stock Details Fetched Successfully", ProductOutStock::with('category', 'brand', 'color', 'size', 'style')->orderBy('product_id', 'desc')->get(), 901);
    }

    public function store(Request $request) {
        $validate = $this->response->validate($request->all(), $this->storeProductManual);
        if($validate === true) {
            $manual_quantity = Product::where('product_id',$request->product_id)->first();
            $request['product_manual_quantity'] =$request->product_manual_quantity + $manual_quantity->product_manual_quantity;
            return $this->response->jsonResponse(false, "Product Manual Quantity Updated Successfully", Product::where('product_id',$request->product_id)->update($request->all()), 902);
        } else {
            return $validate;
        }
    }

    public function show($product_in_stock_id) {
        return $this->response->jsonResponse(false, "Product In Stock Details Showed Successfully", ProductInStock::where('product_in_stock_id',$product_in_stock_id)->with('category', 'brand', 'color', 'size', 'style')->get(), 903);
    }

    public function destroy($product_in_stock_id) {
        $size = $this->findProduct($product_in_stock_id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('Product In Stock Details', 'destroy'), $size->delete(), 904);
        }
        return $this->response->jsonResponse(true, 'Product In Stock Details Not Exists', [], 905);
    }

    public function findProduct($product_id) {
        return ProductInStock::find($product_id);
    }

    public function getActiveProductInStock($product_id) {
        return $this->response->jsonResponse(false, "Product In Stock Details Fetched Successfully", ProductInStock::with('category', 'brand', 'color', 'size', 'style')->where('product_id',$product_id)->where('active_status',1)->orderBy('product_id', 'desc')->get(), 906);
    }

    public function getActiveAllProductInStock() {
        return $this->response->jsonResponse(false, "Product In Stock Active Details Fetched Successfully", ProductInStock::with('category', 'brand', 'color', 'size', 'style')->where('active_status',1)->orderBy('product_id', 'desc')->get(), 907);
    }

    public function productInStockSwitch($product_in_stock_id) {
        $size = $this->findProduct($product_in_stock_id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'Product In Stock Details '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 908);
        }
        return $this->response->jsonResponse(true, 'Product In Stock Details Not Exists', [], 909);
    }

    public function searchInStockProducts(Request $request) {
        // Log::info('searchstock =='. json_encode($request->all()));
        $checkProductInStock = Product::with('category', 'brand', 'style', 'size', 'color')
        ->whereHas('category', function ($query) use ($request){
            $query->where('category_name', 'LIKE', '%'.$request->category_name.'%');
        })->whereHas('brand', function ($query) use ($request){
            $query->where('brand_name', 'LIKE', '%'.$request->brand_name.'%');
        })->whereHas('style', function ($query) use ($request){
            $query->where('style_name', 'LIKE', '%'.$request->style_name.'%');
        })->whereHas('size', function ($query) use ($request){
            $query->where('size_name', 'LIKE', '%'.$request->size_name.'%');
        })->whereHas('color', function ($query) use ($request){
            $query->where('color_name', 'LIKE', '%'.$request->color_name.'%');
        })->exists();
        if($request === "null") {
            return $this->response->jsonResponse(false, $this->response->message('Product Active Stock ', 'search'), [], 910);
        }
        //  else if($request->all() === []) {
        //     return $this->response->jsonResponse(true, $this->response->message('Product In Stock Details Empty ', 'search'), [], 200);
        // }
        else if(!$checkProductInStock) {
            return $this->response->jsonResponse(true, "Product Active Stock Details Not Found", [], 911);
        } else {
            $searchData = Product::with('category', 'brand', 'style', 'size', 'color')
            ->whereHas('category', function ($query) use ($request){
                $query->where('category_name', 'LIKE', '%'.$request->category_name.'%');
            })->whereHas('brand', function ($query) use ($request){
                $query->where('brand_name', 'LIKE', '%'.$request->brand_name.'%');
            })->whereHas('style', function ($query) use ($request){
                $query->where('style_name', 'LIKE', '%'.$request->style_name.'%');
            })->whereHas('size', function ($query) use ($request){
                $query->where('size_name', 'LIKE', '%'.$request->size_name.'%');
            })->whereHas('color', function ($query) use ($request){
                $query->where('color_name', 'LIKE', '%'.$request->color_name.'%');
            })->where('active_status', 1)->orderBy('product_id', 'desc')->get();

            return $this->response->jsonResponse(false, $this->response->message('Product Active Stock Details', 'search'), $searchData, 912);
        }
    }

    public function searchInStock($search) {
        if($search === "null") {
            return $this->response->jsonResponse(false, $this->response->message('Product', 'search'), [], 913);
        }
        $searchData = ProductInStock::with('category', 'brand', 'style', 'size', 'color')
            ->whereHas('category', function ($query) use ($search){
                $query->where('category_name', 'LIKE', '%'.$search.'%');
            })->orWhereHas('brand', function ($query) use ($search){
                $query->where('brand_name', 'LIKE', '%'.$search.'%');
            })->orWhereHas('style', function ($query) use ($search){
                $query->where('style_name', 'LIKE', '%'.$search.'%');
            })->orWhereHas('size', function ($query) use ($search){
                $query->where('size_name', 'LIKE', '%'.$search.'%');
            })->orWhereHas('color', function ($query) use ($search){
                $query->where('color_name', 'LIKE', '%'.$search.'%');
            })->orWhere('product_code', 'LIKE', '%'.$search.'%')->orWhere('product_price', 'LIKE', '%'.$search.'%')->where('active_status', 1)->orderBy('product_id', 'desc')->get();

        return $this->response->jsonResponse(false, "Product In Stock Details", $searchData, 914);
    }

    public function searchOutStock($search) {
        if($search === "null") {
            return $this->response->jsonResponse(false, $this->response->message('Product', 'search'), [], 915);
        }
        $searchData = ProductOutStock::with('category', 'brand', 'style', 'size', 'color')
            ->whereHas('category', function ($query) use ($search){
                $query->where('category_name', 'LIKE', '%'.$search.'%');
            })->orWhereHas('brand', function ($query) use ($search){
                $query->where('brand_name', 'LIKE', '%'.$search.'%');
            })->orWhereHas('style', function ($query) use ($search){
                $query->where('style_name', 'LIKE', '%'.$search.'%');
            })->orWhereHas('size', function ($query) use ($search){
                $query->where('size_name', 'LIKE', '%'.$search.'%');
            })->orWhereHas('color', function ($query) use ($search){
                $query->where('color_name', 'LIKE', '%'.$search.'%');
            })->orWhere('product_code', 'LIKE', '%'.$search.'%')->orWhere('product_price', 'LIKE', '%'.$search.'%')->orderBy('product_id', 'desc')->get();

        return $this->response->jsonResponse(false, "Product Out Stock Details", $searchData, 916);
    }
}
