<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Models\Category;
use App\Models\SubCategory;
use App\Models\SubCategoriesImage;
use App\Models\Products;
use App\Models\ProductsImage;
use App\Repositories\ResponseRepository;
use Illuminate\Validation\Rule;

class ProductsController extends Controller
{
    public function __construct(ResponseRepository $response){
        $this->response = $response;
        $this->storeRules = [
            'category_id' => 'required',
            'sub_category_id' => 'required',
            'product_name' => 'required|unique:products,product_name,NULL,product_id,deleted_at,NULL',
            'description' => 'required',
            'normal_price' => 'required',
            'discount_price' => 'required',
            'best_selling' => 'required',
            'add_information' => 'required',
            'range_price_one' => 'required',
             'range_price_two' => 'required',
             'max_quantity_one' => 'required',
             'max_quantity_two' => 'required',
        ];
        $this->editRules = [
            'category_id' => 'required',
            'sub_category_id' => 'required',
            'product_name' => 'required',
            'description' => 'required',
            'normal_price' => 'required',
            'discount_price' => 'required',
            'best_selling' => 'required',
            'add_information' => 'required',
            'max_quantity_one' => 'required',
             'max_quantity_two' => 'required',
             'range_price_one' => 'required',
             'range_price_two' => 'required',
        ];
    }

    public function indexProducts() {
        return $this->response->jsonResponse(false,"Products Detail Fetched Successfully",  Products::with('categoryList','subCategoryList','productsImages')->orderBy('product_id', 'desc')->get() , 300);
    }

    public function getActiveProducts() {
        return $this->response->jsonResponse(false,"Active Products Detail Fetched Successfully",  Products::with('categoryList','subCategoryList','productsImages')->where('active_status',1)->orderBy('product_id', 'desc')->get() , 502);
    }

    public function store(Request $request) {
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if($validate === true) {
            $Category = Category::where('category_id', $request['category_id'])->first();
            $request['category_slug'] = $Category->category_slug;
            $SubCategory = SubCategory::where('sub_category_id', $request['sub_category_id'])->first();
            $request['sub_category_slug'] = $SubCategory->sub_category_slug;
            $request['product_slug'] = $this->response->generateSlug($request['product_name']);
            return $this->response->jsonResponse(false, $this->response->message('Products', 'store'), Products::create($request->all()), 301);
        } else {
            return $validate;
        }
    }

    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('Products', 'show'), $this->findTestimony($id), 302);
    }

    public function updateProducts(Request $request) {
        $checkProduct = Products::where('category_id', $request->category_id)->where('sub_category_id', $request->sub_category_id)->where('product_id', $request->product_name)
        ->where('normal_price', $request->normal_price)->where('discount_price', $request->discount_price)->exists();
        if(!$checkProduct) {
            $validate = $this->response->validate($request->all(), $this->editRules);
            if($validate === true){
                $Category = Category::where('category_id', $request['category_id'])->first();
                $request['category_slug'] = $Category->category_slug;
                $SubCategory = SubCategory::where('sub_category_id', $request['sub_category_id'])->first();
                $request['sub_category_slug'] = $SubCategory->sub_category_slug;
                $request['product_slug'] = $this->response->generateSlug($request['product_name']);
                return $this->response->jsonResponse(false, $this->response->message('Products', 'update'), $this->findTestimony($request->product_id)->update($request->all()), 303);
            } else {
                return $validate;
            }
        }
        return $this->response->jsonResponse(true, "This Product Already Created ", [], 607);
    }

    public function destroy($id) {
        $size = $this->findTestimony($id);
        if($size) {
            $deleted = ProductsImage::where('product_id',$id)->delete();
            // Log::info('deleted =='. $deleted);
            return $this->response->jsonResponse(false, $this->response->message('Products', 'destroy'), $size->delete(), 304);
        }
        return $this->response->jsonResponse(true, 'Products Not Exists', [], 305);
    }

    public function findTestimony($id) {
        return Products::find($id);
    }

    public function productSwitch($id) {
        $size = $this->findTestimony($id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'Products '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 306);
        }
        return $this->response->jsonResponse(true, 'Products Not Exists', [], 307);
    }

    public function searchProducts($search){
        if($search === "null") {
            return $this->response->jsonResponse(false, $this->response->message('Products', 'search'), [], 910);
        }
        $searchData = Products::with('categoryList','subCategoryList','productsImages')
        ->whereHas('categoryList', function ($query) use ($search){
            $query->where('category_name', 'LIKE', '%'.$search.'%');
        })->orwhereHas('subCategoryList', function ($query) use ($search){
            $query->where('sub_category_name', 'LIKE', '%'.$search.'%');
        })->orWhere('product_name', 'LIKE', '%'.$search.'%')->orderBy('product_id', 'desc')->get();
        return $this->response->jsonResponse(false, $this->response->message('Products', 'search'), $searchData, 911);
    }
}
