<?php

namespace App\Http\Controllers;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class CategoryController extends Controller
{

    public function rules()
    {
        return [
            'category_name' => 'required||unique:category',
        ];
    }

    ##STORE
    public function storecategory(Request $request){
        $checkProduct = Category::where('category_name', $request->category_name)
        ->exists();
        log::info($checkProduct);

       if(!$checkProduct){
        $validator = \Validator::make($request->all(), $this->rules());

        if ($validator->fails()) {
            return response()->json(['status' => false,'message' => 'Category Name all Ready taken'], 200);
        }else{
            $storedata                = new Category();
            $storedata->category_name = $request->category_name;
            $storedata->category_slug = strtolower(str_replace(' ','-',$request->category_name));
            $store                    =  $storedata->save();
            return response()->json(['status' => true,'message' => 'Category data added Successfully'],200);
        }
        }else{
        return response()->json(['status' => false,'message' => 'The  Category name has already been taken.'],200);
        }
        }

    ##GET 
    public function getcategory(){
        $getdata     = category::orderBy('category_id','desc')->get();
        if ($getdata == true){
            return response()->json(['status' => true, 'Records' => $getdata], 200);
        } else {
            return response()->json(['status' => false,'data' => 'failed data'], 400);
        }
    }

    ##SWITCH
    public function switchcategory($id){
        $switchData                  = Category::find($id);
        $status                      = $switchData->active_status === 1 ? ['num' => 0, 'message' => 'Deactiveted'] : ['num' => 1, 'message' => 'Activeted'];
        $switchData->active_status   = $status['num'];
        $Switch                      = $switchData->save();
        if ($Switch) {
            return response()->json(['status' => true,'message'=> $status['message'].'  '.'Successfully'],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed switch '],400);
        }
    }

    ##DESTORY
    public function destroycategory($id){
        $destroy  = Category::where('category_id', $id)->delete();
        if ($destroy == true) {
            return response()->json(['status' => true,'message'=>'destroy Success'],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed destroy'],400);
        }
    }

    ##UPDATE
    public function updatecategory(Request $request){
        // $validate   =   $request->validate([
        //     'category_name' => 'required',
        // ]);
        // $updatedata                 = Category::find($request->id);
        // $updatedata->category_name  = $request->category_name;
        // $updatedata->category_slug  = strtolower(str_replace(' ','-',$request->category_name));
        // $update                     = $updatedata->save();
        // if (!$updatedata){
        //     return response()->json(['status' => false,'message'=>'update data not found'],404);
        // }else if ($update == true) {
        //     return response()->json(['status' => true,'message'=>'update Success'],200);
        // }else{
        //     return response()->json(['status' => false,'message'=>'failed update'],400);
        // }
        $validator = \Validator::make($request->all(), $this->rules());

        if ($validator->fails()) {
            return response()->json(['status' => false,'message' => 'Category Name all Ready taken'], 200);
        }else{
          $updatedata                   = Category::find($request->id);
            $updatedata->category_name  = $request->category_name;
            $updatedata->category_slug  = strtolower(str_replace(' ','-',$request->category_name));
            $update                     = $updatedata->save();
            return response()->json(['status' => true,'message' => 'update Success'],200);
        }

        
    }

    ##FIND
    public function findcategory($id){
        $find  = Category::where('category_id', $id)->orderBy('category_id', 'desc')->get();
        if ($find == true) {
            return response()->json(['status' => true,'data' =>  $find,'message'=>'Find Data Successfully'],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed find records'],400);
        }
    }

    ##SEARCH
    public function searchcategory($name){
        $searchRecord   = Category::where('category_name', 'LIKE', '%'.$name.'%')->get();
        if (!empty($searchRecord)) {
            return response()->json(['status' => true,'message'=>'search Record Successfully','data' =>  $searchRecord],200);
        }else{
            return response()->json(['status' => false,'message'=>'searchRecord Record Failed'],400);
        }
    }
}
