<?php

namespace App\Http\Controllers;

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

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

    public function index() {
        return $this->response->jsonResponse(false,"Dental Service Details Fetched Successfully", DentalService::orderBy('id', 'desc')->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('Dental Service Details', 'store'), DentalService::create($request->all()), 200);
        } else {
            return $validate;
        }
    }

    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('Dental Service Details', 'show'), $this->findDentalService($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('Dental Service Details', 'update'), $this->findDentalService($id)->update($request->all()), 200);
        } else {
            return $validate;
        }
    }

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

    public function findDentalService($id) {
        return DentalService::find($id);
    }

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