<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use App\Models\Gallery;
use App\Repositories\ResponseRepository;

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


    public function indexGallery() {
        // return $this->response->jsonResponse(false,"Gallery Detail Fetched Successfully",  Gallery::with('GalleryImages')->orderBy('gallery_id', 'desc')->get() , 300);
        return $this->response->jsonResponse(false,"Gallery Detail Fetched Successfully",  Gallery::orderBy('gallery_id', 'desc')->get(), 100);
    }

    public function getGalleryDetailsId($gallery_id) {
        // return $this->response->jsonResponse(false, 'Gallery Details Fetched SuccessFully', Gallery::where('gallery_id', $gallery_id)->first(), 201);
                return $this->response->jsonResponse(false, 'Gallery Details Fetched SuccessFully', Gallery::with('GalleryImages')->where('gallery_id', $gallery_id)->first(), 201);


    }

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

    public function store(Request $request) {
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if($validate === true) {
            $request['gallery_slug'] = $this->response->generateSlug($request['gallery_name']);
            return $this->response->jsonResponse(false, $this->response->message('Gallery', 'store'), Gallery::create($request->all()), 301);
        } else {
            return $validate;
        }
    }



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


    public function updateGallery(Request $request) {
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if($validate === true) {
            $request['gallery_slug'] = $this->response->generateSlug($request['gallery_name']);
            return $this->response->jsonResponse(false, $this->response->message('Gallery', 'update'), $this->findTestimony($request->gallery_id)->update($request->all()), 303);
        } else {
            return $validate;
        }
    }

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


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


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

    public function imageUpdateGallery(Request $request) {
        $getBanner = $this->findTestimony($request['gallery_id']);
        if($request->hasFile('gallery_image')) {
            $file = $request->file('gallery_image');
            $name = $this->response->generateSlug($getBanner['gallery_name']);
            $uploadUrl = $this->response->cloudinaryImage($request->file('gallery_image'), 'Gallery', $name);
            return $this->response->jsonResponse(false, $this->response->message('Gallery', 'image'), $getBanner->update(['gallery_image' => $uploadUrl]), 108);
        } else {
            return $this->response->jsonResponse(true, 'Image  is too high', [], 109);
        }
    }



    public function searchGallery($search){
        if($search === "null") {
            return $this->response->jsonResponse(false, $this->response->message('Gallery', 'search'), [], 910);
        }
        $searchData = Gallery::with('categoryList','subCategoryList','GalleryImages')
        ->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('gallery_id', 'desc')->get();
        return $this->response->jsonResponse(false, $this->response->message('Gallery', 'search'), $searchData, 911);
    }






}
