<?php

namespace App\Http\Controllers;

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

class FacilitiesController extends Controller
{
    public function __construct(ResponseRepository $response) {
        $this->response = $response;
        $this->storeRules = ['description' => '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('Facilities', 'store'), Facilities::create($request->all()), 200);
        } else {
            return $validate;
        }
    }

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

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

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

    public function findFacilities($id) {
        return Facilities::find($id);
    }

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

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