<?php

namespace App\Http\Controllers;

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


class SubCategoriesImageController extends Controller
{
    public function __construct(ResponseRepository $response) {
        $this->response = $response;
        $this->storeRules = [
            'sub_category_id' => 'required',
           
            'banner_name' => 'required',
        ];
        $this->editRules = [
            'sub_category_id' => 'required',
            'banner_name' => 'required',
        ];
    }

    public function showBannerById($slug) {
        $data = SubCategoriesImage::where('sub_category_id', $slug )->orderBy('sub_categories_image_id', 'desc')->get();
        return $this->response->jsonResponse(false, " Sub Category Banner Details Fetched Successfully", $data, 200);
    }

    public function store(Request $request) {
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if($validate === true) {

            $SubCategory = SubCategory::where('sub_category_id', $request['sub_category_id'])->first();
            $request['sub_category_slug'] = $SubCategory->sub_category_slug;
            return $this->response->jsonResponse(false, $this->response->message('Sub Category Banners', 'store'), SubCategoriesImage::create($request->all()), 202);
        } else {
            return $validate;
        }
    }

    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('Sub Category Banners', 'show'), $this->findBanner($id), 203);
    }

    // public function updateSubCategoryImage(Request $request) {
    //     $validate = $this->response->validate($request->all(), $this->storeRules);
    //     if($validate === true) {
            
    //         return $this->response->jsonResponse(false, $this->response->message('Sub Category Banner', 'update'), $this->findBanner($request->sub_categories_image_id)->update($request->all()), 204);
    //     } else {
    //         return $validate;
    //     }
    // }

    public function updateSubCategoryImage(Request $request) {
        $checkSubCategoryImage = SubCategoriesImage::where('sub_category_id', $request->sub_category_id)->where('banner_name', $request->banner_name)->exists();
        if(!$checkSubCategoryImage) {
            $validate = $this->response->validate($request->all(), $this->editRules);
            if($validate === true){
               
                $SubCategory = SubCategory::where('sub_category_id', $request['sub_category_id'])->first();
                $request['sub_category_slug'] = $SubCategory->sub_category_slug;
              
                return $this->response->jsonResponse(false, $this->response->message('Sub Category Banner', 'update'), $this->findBanner($request->sub_categories_image_id)->update($request->all()), 204);
            } else {
                return $validate;
            }
        }
        return $this->response->jsonResponse(true, "This Product Already Created ", [], 607);
    }

    public function destroy($id) {
        $size = $this->findBanner($id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('Sub Category Banner', 'destroy'), $size->delete(), 205);
        }
        return $this->response->jsonResponse(true, 'Sub Category Banner Not Exists', [], 206);
    }

    public function findBanner($id) {
        return SubCategoriesImage::find($id);
    }

    public function subCategoryImageSwitch($id) {
        $size = $this->findBanner($id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, $msg.' SuccessFully', $size->update(['active_status' => $value]), 208);
        }
        return $this->response->jsonResponse(true, 'Sub Category Banner List Not Exists', [], 209);
    }

    public function imageUpdatesubCategoriesImage(Request $request) {
        $getBanner = $this->findBanner($request['banner_id']);
        if($request->hasFile('banner_image')) {
            $file = $request->file('banner_image');
            $name = $this->response->generateSlug($getBanner['banner_name']);
            $uploadUrl = $this->response->cloudinaryImage($request->file('banner_image'), 'SubCategory', $name);
            return $this->response->jsonResponse(false, $this->response->message('Sub Category Banner', 'image'), $getBanner->update(['banner_image' => $uploadUrl]), 409);
        } else {
            return $this->response->jsonResponse(true, 'Image Banner is too high', [], 410);
        }
    }
}
