<?php

namespace App\Http\Controllers;

use App\Models\CoursesDescription;
use App\Repositories\ResponseRepository;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
class CoursesDescriptionController extends Controller
{
    public function __construct(ResponseRepository $response) {
        $this->response = $response;
        $this->storeRules = 
        [
            'courses_id' => 'required',
            'courses_name' => 'required',
            'courses_title' => 'required',
            'courses_description' => 'required',
        ];
    }

    public function indexCoursesDescription() {
        return $this->response->jsonResponse(false,"CoursesDescription Details Fetched Successfully", CoursesDescription::with('courseslist')->orderBy('course_description_id', 'desc')->get() , 300);
    }

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


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

    public function updateCoursesDescription(Request $request) {
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if($validate === true) {
            
            $request['courses_slug'] = $this->response->generateSlug($request['courses_name']);
            return $this->response->jsonResponse(false, $this->response->message('CoursesDescription', 'update'), $this->findBanner($request->id)->update($request->all()), 303);
        } else {
            return $validate;
        }
    }

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

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

    public function coursesDescriptionSwitch($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, 'CoursesDescription Not Exists', [], 107);
    }

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