<?php

namespace App\Http\Controllers;

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

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

    public function index() {
        return $this->response->jsonResponse(false, "Category Details Fetched Successfully", Categories::orderBy('category_name', 'ASC')->get(), 100);
    }

    public function store(Request $request) {
        $checkCategory = Categories::where('category_name', $request->category_name)->exists();
        if(!$checkCategory){
            $validate = $this->response->validate($request->all(), $this->storeRules);
            if($validate === true) {
                return $this->response->jsonResponse(false, "Category Created Successfully", Categories::create($request->all()), 101);
            } else {
                return $validate;
            }
        }
        return $this->response->jsonResponse(true, "This ".$request->category_name. " Category Already Created", [], 102);
    }

    public function show($category_id) {
        return $this->response->jsonResponse(false, $this->response->message('Category Details', 'show'), $this->findCategories($category_id), 103);
    }

    // public function update(Request $request, $category_id) {
    //     $checkCategory = Categories::where('category_name', $request->category_name)->exists();
    //     if(!$checkCategory){
    //         $validate = $this->response->validate($request->all(), ['category_name' => ['required' ]]);
    //         if($validate === true) {
    //             return $this->response->jsonResponse(false, $this->response->message('Category Details', 'update'), $this->findCategories($category_id)->update($request->all()), 104);
    //         } else {
    //             return $validate;
    //         }
    //     }
    //     return $this->response->jsonResponse(true, "This ".$request->category_name. " Category Already Created", [], 105);
    // }

    public function updateCategories(Request $request) {
        $validate = $this->response->validate($request->all(), [
            'category_name' => ['required'],
        ]);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Category Details', 'update'), $this->findCategories($request->category_id)->update($request->all()), 303);
        } else {
            return $validate;
        }
    }

    public function destroy($category_id) {
        $size = $this->findCategories($category_id);
        if($size) {
            return $this->response->jsonResponse(false, $this->response->message('Category Details', 'destroy'), $size->delete(), 106);
        }
        return $this->response->jsonResponse(true, 'Category Details Not Exists', [], 107);
    }

    public function findCategories($category_id) {
        return Categories::find($category_id);
    }

    public function getActiveCategories() {
        return $this->response->jsonResponse(false, "Category Details Fetched Successfully", Categories::where('active_status', 1)->orderBy('category_name', 'asc')->get(), 108);
    }

    public function searchCategories($search){
        if($search === "null") {
            return $this->response->jsonResponse(false, "Category Not Found", [], 109);
        }
        return $this->response->jsonResponse(false, $this->response->message('Category', 'search'), Categories::where('category_name', 'LIKE', '%'.$search.'%')->get(), 110);
    }

    public function categoriesSwitch($category_id) {
        $size = $this->findCategories($category_id);
        if($size) {
            $value = $size->active_status === 1 ? 0 : 1;
            $msg = $value === 0 ? 'Deactivated': 'Activated';
            return $this->response->jsonResponse(false, 'Category Details '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 111);
        }
        return $this->response->jsonResponse(true, 'Category Details Not Exists', [], 112);
    }
}
