<?php

namespace App\Http\Controllers;

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

class EmployeeDetailsController extends Controller
{
    public function __construct(ResponseRepository $response){
        $this->response = $response;
        $this->storeRules = [
            'first_name' => 'required',
            'last_name' => 'required',
            'title' => 'required',
            'mobile_no' => 'required',
            'user_id' => 'required',
            'password' => 'required',
            'end_date' => 'required',
        ];
        $this->storeRulestwo = [
            'emp_image' => 'required',
        ];
    }

    public function indexEmployeeDetails() {
        return $this->response->jsonResponse(false," EmployeeDetails Details Fetched Successfully", EmployeeDetails::orderBy('employee_details_id', 'desc')->get() , 300);
    }

    public function getEmployeeDetailsId($employee_details_id) {
        return $this->response->jsonResponse(false, 'EmployeeDetails Details Fetched Successfully', EmployeeDetails::where('employee_details_id', $employee_details_id)->first(), 201);
    }

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

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

    public function imageuploadedEmployee(Request $request) {
        $getBanner = $this->findEmployeeDetails($request['employee_details_id']);
        if($request->hasFile('emp_image')) {
            $file = $request->file('emp_image');
            $name = $this->response->generateSlug($getBanner['first_name']);
            $uploadUrl = $this->response->cloudinaryImage($request->file('emp_image'), 'Banner', $name);
            return $this->response->jsonResponse(false, $this->response->message('Contractor ', 'image'), $getBanner->update(['emp_image' => $uploadUrl]), 108);
        } else {
            return $this->response->jsonResponse(true, 'Image is too high', [], 109);
        }
    }

    public function updateEmployeeDetails(Request $request)
    {
        $validate = $this->response->validate($request->all(), [
            'first_name' => 'required',
            'last_name' => 'required',
            'title' => 'required',
            'mobile_no' => 'required',
            'user_id' => 'required',
            'password' => 'required',
            'end_date' => 'required',
            'active_status' => 'required'
        ]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('EmployeeDetails', 'update'), $this->findEmployeeDetails($request->employee_details_id)->update($request->all()), 303);
        } else {
            return $validate;
        }
    }

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

    public function findEmployeeDetails($id) {
        return EmployeeDetails::find($id);
    }

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

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



}
