<?php

namespace App\Http\Controllers;

// use Illuminate\Http\Request;
// use App\Models\WhoWeAreBanner;
// use App\Repositories\ResponseRepository;
// use Illuminate\Support\Facades\Log;

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


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

    public function indexwhoHomeBanner() {
        return $this->response->jsonResponse(false,"Banner Details Fetched Successfully", WhoWeAreBanner::orderBy('id', 'desc')->get(), 100);
    }

    public function store(Request $request) {
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Banner', 'store'), WhoWeAreBanner::create($request->all()), 101);
        } else {
            return $validate;
        }
    }

   public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('Banner', 'show'), $this->findBanner($id), 102);
    }



    public function updatewhoHomeBanner(Request $request) {
        $validate = $this->response->validate($request->all(), ['banner_name' => ['required']]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Banner', 'update'), $this->findBanner($request->id)->update($request->all()), 103);
        } else {
            return $validate;
        }
    }

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

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

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

    public function imagewhoUpdateHomeBanner(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'), 'Banner', $name);
            return $this->response->jsonResponse(false, $this->response->message('Banner', 'image'), $getBanner->update(['banner_image' => $uploadUrl]), 108);
        } else {
            return $this->response->jsonResponse(true, 'Image Banner is too high', [], 109);
        }
    }
    public function searchwhoHomeBanner($search){
        if($search === "null") {
            return $this->response->jsonResponse(false, $this->response->message('Banner', 'search'), [], 110);
        }
        return $this->response->jsonResponse(false, $this->response->message('Banner', 'search'), WhoWeAreBanner::where('banner_name', 'LIKE', '%'.$search.'%')->get(), 111);
    }


    // public function updateWhoWeAreBanner(Request $request) {
    //     $validate = $this->response->validate($request->all(), ['banner_name' => ['required']]);
    //     if($validate === true) {
    //         return $this->response->jsonResponse(false, $this->response->message('Banner', 'update'), $this->findBanner($request->id)->update($request->all()), 103);
    //     } else {
    //         return $validate;
    //     }
    // }

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

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

    // public function whoWeAreSwitch($id) {
    //     $size = $this->findBanner($id);
    //     if($size) {
    //         $value = $size->active_status === 1 ? 0 : 1;
    //         $msg = $value === 0 ? 'Deactivated': 'Activated';
    //         return $this->response->jsonResponse(false, 'Banner '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 106);
    //     }
    //     return $this->response->jsonResponse(true, 'HomeBanner Not Exists', [], 107);
    // }

    // public function imageUpdateWhoWeAreBanner(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'), 'Banner', $name);
    //         return $this->response->jsonResponse(false, $this->response->message('Banner', 'image'), $getBanner->update(['banner_image' => $uploadUrl]), 108);
    //     } else {
    //         return $this->response->jsonResponse(true, 'Image Banner is too high', [], 109);
    //     }
    // }

    // public function searchWhoWeAreBanner($search){
    //     if($search === "null") {
    //         return $this->response->jsonResponse(false, $this->response->message('Banner', 'search'), [], 110);
    //     }
    //     return $this->response->jsonResponse(false, $this->response->message('Banner', 'search'), WhoWeAreBanner::where('banner_name', 'LIKE', '%'.$search.'%')->get(), 111);
    // }
}
