<?php

namespace App\Http\Controllers;

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

class UserTypesController extends Controller
{
    public function __construct(ResponseRepository $response) {
        $this->response = $response;
        $this->storeRules = ['type_name' => 'required',];
    }

    public function index() {
        return $this->response->jsonResponse(false,"User Types Details Fetched Successfully", UserTypes::where('active_status', 1)->get(), 200);
    }

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

    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('User Types Details', 'show'), $this->findUserTypes($id), 200);
    }

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

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

    public function findUserTypes($id) {
        return UserTypes::find($id);
    }

    public function getActiveUserTypes() {
        return $this->response->jsonResponse(false, "User Types Details Fetched Successfully", UserTypes::where('active_status', 1)->orderBy('id', 'desc')->get(), 200);
    }

    public function getAllUserTypes() {
        return $this->response->jsonResponse(false, $this->response->message('User Types Details', 'index'), UserTypes::orderBy('id', 'desc')->get(), 200);
    }

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