<?php

namespace App\Http\Controllers;


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',];
    }
    // All Data Get Function
    public function getAllGalleryData() {
        return $this->response->jsonResponse(false,"Gallery Details Fetched Successfully", Gallery::orderBy('id', 'desc')->get(), 100);
    }
    // Data Store Function
    public function store(Request $request) {
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Gallery', 'store'), Gallery::create($request->all()), 101);
        } else {
            return $validate;
        }
    }
    // Particular id data get function 
    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('Gallery', 'show'), $this->findGallery($id), 102);
    }
    // Update Function 
    public function updateGalleryData(Request $request) {
        $validate = $this->response->validate($request->all(), ['gallery_name' => ['required']]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Gallery', 'update'), $this->findGallery($request->id)->update($request->all()), 103);
        } else {
            return $validate;
        }
    }
    //  Delete Function
    public function destroy($id) {
        $size = $this->findGallery($id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('Gallery', 'destroy'), $size->delete(), 104);
        }
        return $this->response->jsonResponse(true, 'Gallery Not Exists', [], 105);
    }
    // Id Find Function
    public function findGallery($id) {
        return Gallery::find($id);
    }
    // Active Deactive Function
    public function activeDeactiveGalleryData($id) {
        $size = $this->findGallery($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]), 106);
        }
        return $this->response->jsonResponse(true, 'Gallery Not Exists', [], 107);
    }
    // Image Update Function
    public function imageUpdateGalleryPage(Request $request) {
        $getBanner = $this->findGallery($request['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'), 'GalleryPage', $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);
        }
    }
    //  Search Function
    public function searchGalleryData($search){
        if($search === "null") {
            return $this->response->jsonResponse(false, $this->response->message('Gallery', 'search'), [], 110);
        }
        return $this->response->jsonResponse(false, $this->response->message('Gallery', 'search'), Gallery::where('gallery_name', 'LIKE', '%'.$search.'%')->get(), 111);
    }
   
}
