<?php

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


class SubcategoryController extends Controller
{
    ## ACTIVE CATEGORY LIST
    public function getactivecategorylist(){
        $getcategorylist   = Category::orderBy('category_id', 'desc')->where('active_status','1')->get();
        if ($getcategorylist == true){
            return response()->json(['status' => true,'message' => 'Category Data','data'=> $getcategorylist ],200);
        }else{
            return response()->json(['status' => false,'data' => 'failed data'],200);
        }
    }
   
    ## STORE 
    public function storesubcategory(Request $request){
        $checkProduct = Subcategory::where('subcategory_name', $request->subcategory_name)
        ->exists();
        log::info($checkProduct);

       if(!$checkProduct){
        $validate   =   $request->validate([
            'category_id'       => 'required',
            'subcategory_name'  => 'required',
        ]);
        $categoryDetails             =  Category::orderBy('category_id', 'desc')->where('category_id',$request->category_id)->first();
        $category_slug               =  $categoryDetails->category_slug;
        $storedata                   =  new Subcategory();
        $storedata->category_id      =  $request->category_id;
        $storedata->subcategory_name =  $request->subcategory_name;
        $storedata->category_slug    =  $category_slug;
        $storedata->subcategory_slug =  strtolower(str_replace(' ','-',$request->subcategory_name));
        $store                       =  $storedata->save();
        if ($store == true){
            return response()->json(['status' => true,'data' => 'Subcategory data added Successfully'],200);
        }else{
            return response()->json(['status' => false,'data' => 'failed Store'],200);
        }
    }else{
        return response()->json(['status' => false,'message' => 'The Sub Category name has already been taken.'],200);
    }
    }

    ## SWITCH
    public function switchsubcategory($id){
        $switchData                  = Subcategory::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);
        }
    }

    ## UPDATE
    public function updatesubcategory(Request $request){
        $validate   =   $request->validate([
            'category_id'       => 'required',
            'subcategory_name'  => 'required',
        ]);
        $updatedata                    = Subcategory::find($request->id);
        $updatedata->category_id       = $request->category_id;
        $updatedata->subcategory_name  = $request->subcategory_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);
        }
    }

    ##FIND
    public function findsubcategory($id){
        $find  = Subcategory::where('subcategory_id', $id)->get();
        if ($find->isNotEmpty()){
            return response()->json(['status' => true,'message'=>'Find Data Successfully','data' =>  $find],200);
        }else if($find->isEmpty()){
            return response()->json(['status' => true,'message'=>'data is empty'],204);
        }else{
            return response()->json(['status' => false,'message'=>'failed find records'],400);
        }
    }

    ##SEARCH
    public function searchsubcategory($name){
        if ($searchRecord->isNotEmpty()){
            return response()->json(['status' => true,'message'=>'search Record Successfully','data' =>  $searchRecord],200);
        }else if($searchRecord->isEmpty()){
            return response()->json(['status' => true,'message'=>'search Record Empty'],204);
        }else{
            return response()->json(['status' => false,'message'=>'searchRecord Record Failed'],400);
        }
    }

    ## GET
    public function getsubcategory(){
        $getdata     = Subcategory::with('categoryDetails')->get();
        if ($getdata->isNotEmpty()){
            return response()->json(['status' => true, 'data' => $getdata], 200);
        } else {
            return response()->json(['status' => false,'data' => 'failed data'], 400);
        }
    }


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

}
