<?php

namespace App\Http\Controllers;

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

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

    public function index() {
        return $this->response->jsonResponse(false,"Consultant Details Fetched Successfully", Consultant::orderBy('id', 'desc')->get(), 200);
    }

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

    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('Consultant ', 'show'), $this->findGalleryImage($id), 200);
    }

    public function update(Request $request, $id) {
        $validate = $this->response->validate($request->all(), ['name' => ['required']]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Consultant ', 'update'), $this->findGalleryImage($id)->update($request->all()), 200);
        } else {
            return $validate;
        }
    }

    public function destroy($id) {
        $size = $this->findGalleryImage($id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('Consultant ', 'destroy'), $size->delete(), 200);
        }
        return $this->response->jsonResponse(true, 'Consultant Image Not Exists', [], 201);
    }

    public function findGalleryImage($id) {
        return Consultant::find($id);
    }

    public function consultantSwitch($id) {
        $size = $this->findGalleryImage($id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'Consultant '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 200);
        }
        return $this->response->jsonResponse(true, 'Gallery Image Not Exists', [], 201);
    }

    public function imageUpdateConsultant(Request $request) {
        $getBanner = $this->findGalleryImage($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'), 'Consultant', $name);
            return $this->response->jsonResponse(false, $this->response->message('Clinic Gallery', 'image'), $getBanner->update(['image' => $uploadUrl]), 201);
        } else {
            return $this->response->jsonResponse(true, 'Image Banner is too high', [], 201);
        }
    }
}
