<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use App\Models\WhyZap;
use App\Models\AboutusPage;
use App\Repositories\ResponseRepository;

class WhyZapController extends Controller
{
    public function __construct(ResponseRepository $response){
        $this->response = $response;
        $this->storeRules = [
            'why_zap_title' => 'required',
        ];
        $this->ruleUpdate = [
            'why_zap_heading' => 'required',
            'why_zap_description' => 'required',
        ];
        $this->ruleThree = [
            'why_zap_second_section_heading' => 'required',
            'why_zap_second_section_description' => 'required',
            'why_zap_second_section_description_two' => 'required',

        ];
    }
    // Data Update Function
    public function updateAboutWhyZapContent(Request $request) {
        $validate = $this->response->validate($request->all(), $this->ruleUpdate);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Content', 'update'), AboutusPage::first()->update($request->all()), 551);
        } else {
            return $this->response->jsonResponse(true, 'Heading Update Failed', $validate, 552);
        }
    }
    // Data Update Function
    public function updateAboutWhyZapSectionTwoContent(Request $request) {
        $validate = $this->response->validate($request->all(), $this->ruleThree);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Content', 'update'), AboutusPage::first()->update($request->all()), 551);
        } else {
            return $this->response->jsonResponse(true, 'Heading Update Failed', $validate, 552);
        }
    }
    public function getAboutAboiutPageWhyZapSecondSectionData() {
        $data['heading'] = AboutusPage::select('why_zap_second_section_heading','why_zap_second_section_description','why_zap_second_section_description_two', 'id')->get();
        return $this->response->jsonResponse(false,"Why Zap Details Fetched Successfully", $data , 300);
    }
    // Data Get Function
    public function getAboutPageWhyZapSectionData() {
        $data['heading'] = AboutusPage::select('why_zap_heading','why_zap_description', 'id')->get();
        $data['whyZap'] = WhyZap::orderBy('id', 'desc')->get();
        return $this->response->jsonResponse(false,"Why Zap Details Fetched Successfully", $data , 300);
    }
    // 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('WhyZap', 'store'), WhyZap::create($request->all()), 301);
        } else {
            return $validate;
        }
    }
    // Particular id data get function 
    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('WhyZap', 'show'), $this->findWhyZap($id), 302);
    }
    // Update Function 
    public function updateAboutPageWhyZapSectionData(Request $request) {
        $validate = $this->response->validate($request->all(), ['why_zap_title' => ['required']]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('WhyZap', 'update'), $this->findWhyZap($request->id)->update($request->all()), 103);
        } else {
            return $validate;
        }
    }
    //  Delete Function
    public function destroy($id) {
        $size = $this->findWhyZap($id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('WhyZap', 'destroy'), $size->delete(), 104);
        }
        return $this->response->jsonResponse(true, 'WhyZap Not Exists', [], 105);
    }
    // Id Find Function
    public function findWhyZap($id) {
        return WhyZap::find($id);
    }
    // Active Deactive Function
    public function activeDeactiveWhyZapSectionData($id) {
        $size = $this->findWhyZap($id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'WhyZap '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 106);
        }
        return $this->response->jsonResponse(true, 'WhyZap Not Exists', [], 107);
    }
    // Image Update Function
    public function imageUpdateWhyZap(Request $request) {
        $getBanner = $this->findWhyZap($request['id']);
        if($request->hasFile('why_zap_image')) {
            $file = $request->file('why_zap_image');
            $name = $this->response->generateSlug($getBanner['why_zap_title']);
            $uploadUrl = $this->response->cloudinaryImage($request->file('why_zap_image'), 'WhyZapSection', $name);
            return $this->response->jsonResponse(false, $this->response->message('WhyZap', 'image'), $getBanner->update(['why_zap_image' => $uploadUrl]), 108);
        } else {
            return $this->response->jsonResponse(true, 'Image is too high', [], 109);
        }
    }
    //  Search Function
    public function searchWhyZapSectionData($search){
        if($search === "null") {
            return $this->response->jsonResponse(false, $this->response->message('WhyZap', 'search'), [], 110);
        }
        return $this->response->jsonResponse(false, $this->response->message('WhyZap', 'search'), WhyZap::where('why_zap_title', 'LIKE', '%'.$search.'%')->get(), 111);
    }
    
}
