<?php

namespace App\Http\Controllers;

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

class WhyCrCoilsController extends Controller
{
    //
    public function __construct(ResponseRepository $response){
        $this->response = $response;
        $this->storeRules = [
            'why_cr_coils_mt' => 'required',
            'why_cr_coils_icon' => 'required',
            'why_cr_coils_st' => 'required',
        ];
    }

    public function indexWhyCrCoils() {
        return $this->response->jsonResponse(false," Why Cr Coils Details Fetched Successfully", WhyCrCoils::orderBy('id', 'desc')->get(), 300);
    }

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

    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('Why Cr Coils', 'show'), $this->findWhyCrCoils($id), 302);
    }

    public function updateWhyCrCoils(Request $request) {
        $validate = $this->response->validate($request->all(), [
            'why_cr_coils_mt' => ['required'],
            'why_cr_coils_icon' => ['required'],
            'why_cr_coils_st' => ['required'],
        ]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Why Cr Coils', 'update'), $this->findWhyCrCoils($request->id)->update($request->all()), 303);
        } else {
            return $validate;
        }
    }

    public function destroy($id) {
        $size = $this->findWhyCrCoils($id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('Why Cr Coils', 'destroy'), $size->delete(), 304);
        }
        return $this->response->jsonResponse(true, 'Why Cr Coils Not Exists', [], 305);
    }
    public function findWhyCrCoils($id) {
        return WhyCrCoils::find($id);
    }

    public function whyCrCoilsSwitch($id) {
        $size = $this->findWhyCrCoils($id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'Why Cr Coils '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 306);
        }
        return $this->response->jsonResponse(true, 'Why Cr Coils Not Exists', [], 307);
    }


}
