<?php

namespace App\Http\Controllers;
use App\Models\Addons;
use Illuminate\Http\Request;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddonsController extends Controller
{
    public function rules()
    {
        return [
            'addons_name' => 'required||unique:addons',
        ];
    }

    ##STORE
    public function storeaddons(Request $request){
        $validator = \Validator::make($request->all(), $this->rules());

        if ($validator->fails()) {
            return response()->json(['status' => false,'message' => 'Addons Name all Ready taken'], 200);
        }else{
            $storedata                  = new Addons();
            $storedata->addons_name     = $request->addons_name;
            $storedata->addons_slug     = strtolower(str_replace(' ','-',$request->addons_name));
            $store                      = $storedata->save();
           
            $tableName                  = strtolower(str_replace(' ', '_', $request->addons_name));
            $id                         = $tableName.'_id';
            $idColumn                   = $id; 
            $createTable                = Schema::create($tableName, function(Blueprint $table) use ($idColumn) {
                                                $table->unsignedBigInteger($idColumn)->primary(); 
                                                $table->timestamps();
                                          });

            return response()->json(['status' => true,'message' => 'addons data added Successfully'],200);
        }
    }

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

    ##GET 
    public function getaddonsactivelist(){
        $getdata     = Addons::where('active_status','1')->orderBy('addons_id','desc')->get();
        if ($getdata == true){
            return response()->json(['status' => true, 'data' => $getdata], 200);
        } else {
            return response()->json(['status' => false,'data' => 'failed data'], 400);
        }
    }

    ##SWITCH
    public function switchaddons($id){
        $switchData                  = Addons::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 destroyaddons($id){
        $destroy  = Addons::where('addons_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);
        }
    }


     ##FIND
     public function findaddons($id){
        $find  = Addons::where('addons_id', $id)->orderBy('addons_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 searchaddons($name){
        $searchRecord   = Addons::where('addons_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);
        }
    }

    ## DYNAMIC CREATE COLUMN
    public function createcolumn(Request $request){
        $fieldName     = $request->column_name;
        $tableName     = strtolower(str_replace(' ', '_', $request->addons_name)); 
        if (!empty($request->price)){
            $price           = $request->price;
            $createcolumns   = Schema::table($tableName, function(Blueprint $table) use ($price)  {
                                                $table->integer($price)->nullable(); 
                                          });
        } 
        $createcolumns      = Schema::table($tableName, function(Blueprint $table) use ($fieldName)  {
                                                $table->string($fieldName)->nullable(); 
                                        });  
        return response()->json(['status' => true,'message'=> 'Created Column Successfully'],200);
    }

    ## GET COLUMN NAEMS
    public function getcolumnnames(Request $request){
        $tableName   = strtolower(str_replace(' ', '_', $request->addons_name));
        $columns     = \DB::getSchemaBuilder()->getColumnListing($tableName);
        if ($columns == true) {
            return response()->json(['status' => true,'data' =>  $columns,'message'=>'Find Data Successfully'],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed find records'],400);
        }
    }

}
