<?php

namespace App\Http\Controllers;

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

class DesignationController extends Controller
{
    public function __construct(ResponseRepository $response){
        $this->response = $response;
        $this->storeRules = [
            'desination_name' => 'required',
        ];
    }
    public function indexDesignation() {
        return $this->response->jsonResponse(false," Designation Details Fetched Successfully", Designation::orderBy('desination_id', 'desc')->get(), 300);
    }

    public function getActiveDesignation() {
        return $this->response->jsonResponse(false,"Designation Detail Fetched Successfully",  Designation::where('active_status',1)->orderBy('desination_id', 'desc')->get() , 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('Designation', 'store'), Designation::create($request->all()), 301);
        } else {
            return $validate;
        }
    }

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

    public function updateDesignation(Request $request) {
        $validate = $this->response->validate($request->all(), [
            'desination_name' => ['required'],

        ]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Designation', 'update'), $this->findDesignation($request->id)->update($request->all()), 303);
        } else {
            return $validate;
        }
    }

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

    public function designationSwitch($id) {
        $size = $this->findDesignation($id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'Designation '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 306);
        }
        return $this->response->jsonResponse(true, 'Designation Not Exists', [], 307);
    }

    public function searchDesignation($search){
        if($search === "null") {
            return $this->response->jsonResponse(false, $this->response->message('Designation', 'search'), [], 910);
        }
        return $this->response->jsonResponse(false, $this->response->message('Designation', 'search'), Designation::where('desination_name', 'LIKE', '%'.$search.'%')->get(), 911);
    }

}
