<?php

namespace App\Http\Controllers;

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

class ConsultUsController extends Controller
{
    public function __construct(ResponseRepository $response) {
        $this->response = $response;
        $this->storeRules = [
            'branch' => 'required',
            'address' => 'required',
            // 'mobile_no' => 'required',
            'map_link' => 'required'
        ];
    }

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

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

    public function update(Request $request, $id) {
        $validate = $this->response->validate($request->all(), $this->storeRules);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Consult Us', 'update'), $this->findConsultUs($id)->update($request->all()), 200);
        } else {
            return $validate;
        }
    }

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

    public function findConsultUs($id) {
        return ConsultUs::find($id);
    }

    public function getAllConsultUs() {
        return $this->response->jsonResponse(false, $this->response->message('Consult Us', 'index'), ConsultUs::orderBy('id', 'desc')->get(), 200);
    }

    public function getActiveConsultUs() {
        return $this->response->jsonResponse(false, $this->response->message('Consult Us', 'index'), ConsultUs::where('active_status',1)->orderBy('id', 'desc')->get(), 200);
    }

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