<?php

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

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

    public function indexTestimony() {
        $data['homepageTitle'] = AboutUs::select('header_six', 'id')->get();
        $data['testimony'] = Testimony::orderBy('id', 'desc')->get();
        return $this->response->jsonResponse(false,"Testimony Details Fetched Successfully", $data , 300);
    }

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

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

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

    public function destroy($id) {
        $size = $this->findTestimony($id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('Testimony', 'destroy'), $size->delete(), 304);
        }
        return $this->response->jsonResponse(true, 'Testimony Not Exists', [], 305);
    }

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

    public function TestimonySwitch($id) {
        $size = $this->findTestimony($id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'Testimony '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 306);
        }
        return $this->response->jsonResponse(true, 'Testimony Not Exists', [], 307);
    }
    public function imageUpdateTestimony(Request $request) {
        $getBanner = $this->findTestimony($request['banner_id']);
        if($request->hasFile('banner_image')) {
            $file = $request->file('banner_image');
            $name = $this->response->generateSlug($getBanner['parent_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 searchTestimony($search){
        if($search === "null") {
            return $this->response->jsonResponse(false, $this->response->message('Banner', 'search'), [], 910);
        }
        return $this->response->jsonResponse(false, $this->response->message('Banner', 'search'), Testimony::where('author_name', 'LIKE', '%'.$search.'%')->get(), 911);
    }
}
