<?php

namespace App\Http\Controllers\Api\v2;

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Languages;
use App\Models\CategoryDetails;
use App\Models\Categories;
use App\Models\Videos;
use App\Models\VideoDetails;
use App\Models\UserVideos;
use App\Models\Difficulties;
use App\Models\AgeManagement;
use App\Models\Article_master;
use App\Models\Article;
use App\Models\Product_details;
use App\Models\Product_master;
use App\Models\Feedback;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Models\User;
use App\Models\Subscription;
use App\Models\Transaction;
use App\Models\Packages;
use App\Models\Master_cms;
use App\Models\Cms;
use App\Models\Guide;
use App\Models\Guide_master;
use App\Models\Notification_management;
use Illuminate\Support\Facades\Mail;
use URL;
use Image;
use DB;
use Srmklive\PayPal\Services\ExpressCheckout;

class VideoController extends Controller
{
 protected $provider;
    public function __construct()
    {
        $this->middleware('auth:api');
		$this->provider = new ExpressCheckout();
    }

    
   public function getAllVideos(Request $request)
   {
        $output = array();
        $inputs['header']['authorization'] = $request->header('Authorization');
        $inputs['header']['lang'] = $request->header('Accept-Language');
        $inputs['header']['accept'] = $request->header('Accept');
        $inputs['body'][] = '';
        $folder = public_path('language/'.$inputs['header']['lang'].'.js');
        $json = file_get_contents($folder);
        $obj = json_decode($json, TRUE);
        $lang = Languages::where('code',$inputs['header']['lang'])->first();
        $langId = $lang->id; 
       
        $category = Categories::leftJoin('category_details', function ($join) {
            $join->on('categories.id', '=', 'category_details.category_id');
        })->select(
                'categories.id as id',
                'category_details.name as name',
                'categories.image as image',
                'categories.status as status'
            )
            ->where('category_details.language_id', '=', $langId)
            ->where('categories.status', 1)
            ->get();

            // foreach($category as $cat){
            //     //dd($cat);
            //     //$video_details  = Videos::leftJoin('video_details')
            //      $catVid = Videos::leftJoin('categories', function ($join) {
            //         $join->on('videos.category_id', '=', 'categories.id');
            //         })->leftJoin('category_details', function ($join) {
            //             $join->on('category_details.category_id', '=', 'categories.id');
            //             })
            //         ->select(
            //             'categories.id as id',
            //             'category_details.name as name',
            //             'categories.image as image',
            //             'categories.status as status'
            //         )
            //         ->where('videos.category_id', '=', $cat->id)
            //         ->where('category_details.language_id', '=', $langId)
            //         ->where('categories.status', 1)->get();

            //         // ->with(['videoDetails' => function($query) use($langId){
            //         //     $query->leftJoin('video_details', function ($joining) use($langId) {
            //         //                 $joining->on('videos.id', '=', 'video_details.video_id')->where('language_id', '=', $langId);    
            //         //             })->orderBy('video_details.created_at', 'DESC')->limit(10);
            //         //         }])->get();
            //         dd($catVid);
            // }
        
            // DB::enableQueryLog();

        $catVid = Categories::leftJoin('category_details', function ($join) {
                    $join->on('categories.id', '=', 'category_details.category_id');
                    })->select(
                        'categories.id as id',
                        'category_details.name as name',
                        'categories.image as image',
                        'categories.status as status'
                    )
                    ->where('category_details.language_id', '=', $langId)
                    ->where('categories.status', 1)
                    ->with(['videoDetails' => function($query) use($langId){
                    $query->leftJoin('video_details', function ($joining) use($langId) {
                            $joining->on('videos.id', '=', 'video_details.video_id')->where('language_id', '=', $langId);    
                        })->orderBy('video_details.created_at', 'DESC')->count();
                    }])->limit(10)->get();
            
                    
        if($category && $catVid)
        {
            $output['category'] = $category;
            $output['video'] = $catVid;

            $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
            return response()->json($response);
        }
        else
        {
            $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> []);
            return response()->json($response);
        }
   }


   public function getAllDifficulty(Request $request)
   {
        $output = array();
        $inputs['header']['authorization'] = $request->header('Authorization');
        $inputs['header']['lang'] = $request->header('Accept-Language');
        $inputs['header']['accept'] = $request->header('Accept');
        $inputs['body'][] = '';
        $folder = public_path('language/'.$inputs['header']['lang'].'.js');
        $json = file_get_contents($folder);
        $obj = json_decode($json, TRUE);
        $lang = Languages::where('code',$inputs['header']['lang'])->first();
        $langId = $lang->id; 
       
       
        $diff = Difficulties::all();
       
                    
        if($diff)
        {
            $output['list'] = $diff;
            
            $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
            return response()->json($response);
        }
        else
        {
            $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> []);
            return response()->json($response);
        }
   }

   public function getAllAges(Request $request)
   {
        $output = array();
        $inputs['header']['authorization'] = $request->header('Authorization');
        $inputs['header']['lang'] = $request->header('Accept-Language');
        $inputs['header']['accept'] = $request->header('Accept');
        $inputs['body'][] = '';
        $folder = public_path('language/'.$inputs['header']['lang'].'.js');
        $json = file_get_contents($folder);
        $obj = json_decode($json, TRUE);
        $lang = Languages::where('code',$inputs['header']['lang'])->first();
        $langId = $lang->id; 
       
       
        $ages = AgeManagement::all();
       
                    
        if($ages)
        {
            $output['list'] = $ages;
            
            $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
            return response()->json($response);
        }
        else
        {
            $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> []);
            return response()->json($response);
        }
   }

   public function getFilterVideos(Request $request)
   {
        $output = array();
        $inputs['header']['authorization'] = $request->header('Authorization');
        $inputs['header']['lang'] = $request->header('Accept-Language');
        $inputs['header']['accept'] = $request->header('Accept');
        $inputs['body']['difficulty_id'] = $request->get('difficulty_id');
        $inputs['body']['start_age'] = $request->get('start_age');
        $inputs['body']['end_age'] = $request->get('end_age');
        $inputs['body']['cat_id'] = $request->get('cat_id');
        $inputs['body']['offset'] = $request->get('offset');
        $cat_id = $inputs['body']['cat_id'];
        $offset = $inputs['body']['offset'];
        $folder = public_path('language/'.$inputs['header']['lang'].'.js');
        $json = file_get_contents($folder);
        $obj = json_decode($json, TRUE);
        $lang = Languages::where('code',$inputs['header']['lang'])->first();
        $langId = $lang->id; 

        if($cat_id)
        {
                        
            if($inputs['body']['difficulty_id'] == '' && $inputs['body']['start_age'] == '' && $inputs['body']['end_age'] == '')
            {
                $category = Categories::leftJoin('category_details', function ($join) {
                $join->on('categories.id', '=', 'category_details.category_id');
                })->select(
                        'categories.id as id',
                        'category_details.name as name',
                        'categories.image as image',
                        'categories.status as status'
                    )
                    ->where('category_details.language_id', '=', $langId)
                    ->where('categories.status', 1)
                    ->get();

                if($cat_id == 'all')
                {
                   // dd(1);
                    $catVid = Categories::leftJoin('category_details', function ($join) {
                    $join->on('categories.id', '=', 'category_details.category_id');
                    })->select(
                        'categories.id as id',
                        'category_details.name as name',
                        'categories.image as image',
                        'categories.status as status'
                    )
                    ->where('category_details.language_id', '=', $langId)
                    ->where('categories.status', 1)
                    ->with(['videoDetails' => function($query) use($langId){
                        $query->leftJoin('video_details', function ($joining) use($langId) {
                            $joining->on('videos.id', '=', 'video_details.video_id')->where('language_id', '=', $langId);    
                        })->orderBy('videos.difficulty_id', 'ASC')->orderBy('videos.start_age', 'ASC')->orderBy('video_details.created_at', 'DESC')->limit(6);
                    }])->get();
                    //print_r(DB::getQueryLog());die();
                }
                else
                {
                    //dd(2);
                    $catVid = Categories::leftJoin('category_details', function ($join) {
                        $join->on('categories.id', '=', 'category_details.category_id');
                        })->select(
                            'categories.id as id',
                            'category_details.name as name',
                            'categories.image as image',
                            'categories.status as status'
                        )
                        ->where('category_details.language_id', '=', $langId)
                        ->where('categories.status', 1)
                        ->where('categories.id', $cat_id)
                        ->with(['videoDetails' => function($query) use($langId, $cat_id, $offset){
                                $query->leftJoin('video_details', function ($joining) use($langId, $cat_id, $offset) {
                                $joining->on('videos.id', '=', 'video_details.video_id')->where('language_id', '=', $langId)->where('category_id', $cat_id);     
                                })->orderBy('videos.difficulty_id', 'ASC')->orderBy('videos.start_age', 'ASC')->orderBy('video_details.created_at', 'DESC')->skip($offset)->take(10);
                        }])->get();
                }  
                if($category && $catVid)
                {
                    //dd(3);
                    $output['video'] = $catVid;

                    $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
                    return response()->json($response);
                }
                else
                {
                    //dd(4);
                    $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> []);
                    return response()->json($response);
                }
            }
           elseif($inputs['body']['difficulty_id'] && $inputs['body']['start_age'] == '' && $inputs['body']['end_age'] == '')
            {
                //dd(5);
                return $this->difficultyFilter($inputs['body']['difficulty_id'], $langId, $cat_id, $offset);
            }
            elseif($inputs['body']['start_age'] !='' && $inputs['body']['end_age'] != '' && $inputs['body']['difficulty_id'] == '')
            {
                //dd(6);
                return $this->ageFilter($inputs['body']['start_age'],$inputs['body']['end_age'], $langId, $cat_id, $offset);
            }
            else
            {  
               // dd(7);
               return $this->bothFilter($inputs['body']['start_age'],$inputs['body']['end_age'], $inputs['body']['difficulty_id'], $langId, $cat_id, $offset);   
            }
            
       }
       else
       {
           //dd(8);
            $response = array('status'=>201, 'message'=>$obj['category_id_required'], 'result'=> (object)[]);
            return response()->json($response);
       }    
   }

   public function difficultyFilter($id, $langId, $cat_id, $offset)
   {
       //dd("Hello");
        $output = array();
        $difficulty_id = $id;
        $lang = Languages::find($langId);
        $lang_id = $lang->code;
        
        $folder = public_path('language/'.$lang_id.'.js');
        $json = file_get_contents($folder);
        $obj = json_decode($json, TRUE);
        

        $category = Categories::leftJoin('category_details', function ($join) {
            $join->on('categories.id', '=', 'category_details.category_id');
            })->select(
                    'categories.id as id',
                    'category_details.name as name',
                    'categories.image as image',
                    'categories.status as status'
                )
                ->where('category_details.language_id', '=', $langId)
                ->where('categories.status', 1)
                ->get();

        if($cat_id == 'all')
        {
            $catVidDiff = Categories::leftJoin('category_details', function ($join) {
                $join->on('categories.id', '=', 'category_details.category_id');
                })->select(
                    'categories.id as id',
                    'category_details.name as name',
                    'categories.image as image',
                    'categories.status as status'
                )
                ->where('category_details.language_id', '=', $langId)
                ->where('categories.status', 1)
                ->with(['videoDetails' => function($query) use($langId, $difficulty_id){
                        $query->join('video_details', function ($joining) use($langId, $difficulty_id) {
                            // dd($difficulty_id);
                            $joining->on('videos.id', '=', 'video_details.video_id')->where('video_details.language_id', '=', $langId)->where('videos.difficulty_id', $difficulty_id);      
                        })->orderBy('video_details.created_at', 'DESC')->limit(6);
                }])->get();
        }  
        else
        {
            $catVidDiff = Categories::leftJoin('category_details', function ($join) {
                $join->on('categories.id', '=', 'category_details.category_id');
                })->select(
                    'categories.id as id',
                    'category_details.name as name',
                    'categories.image as image',
                    'categories.status as status'
                )
                ->where('category_details.language_id', '=', $langId)
                ->where('categories.status', 1)
                ->where('categories.id', $cat_id)
                ->with(['videoDetails' => function($query) use($langId, $cat_id, $difficulty_id, $offset){
                        $query->join('video_details', function ($joining) use($langId, $cat_id, $difficulty_id, $offset) {
                            // dd($difficulty_id);
                            $joining->on('videos.id', '=', 'video_details.video_id')->where('video_details.language_id', '=', $langId)->where('videos.category_id', $cat_id)->where('videos.difficulty_id', $difficulty_id);
                            
                        })->orderBy('video_details.created_at', 'DESC')->skip($offset)->take(10);
                }])->get();
        }          
        
               
        if($category && $catVidDiff)
        {
            // $output['category'] = $category;
            $output['video_diff'] = $catVidDiff;

            $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
            return response()->json($response);
        }
        else
        {
            $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> []);
            return response()->json($response);
        }
   }

   public function ageFilter($start_age,$end_age, $langId, $cat_id, $offset)
   {

        $output = array();
        $lang = Languages::find($langId);
        $lang_id = $lang->code;
        $folder = public_path('language/'.$lang_id.'.js');
        $json = file_get_contents($folder);
        $obj = json_decode($json, TRUE);
        $start_age_arr = explode(",",$start_age);
        $catVidAge_arr = [];
        

        $category = Categories::leftJoin('category_details', function ($join) {
            $join->on('categories.id', '=', 'category_details.category_id');
            })->select(
                    'categories.id as id',
                    'category_details.name as name',
                    'categories.image as image',
                    'categories.status as status'
                )
                ->where('category_details.language_id', '=', $langId)
                ->where('categories.status', 1)
                ->get();
        if($cat_id == 'all')
        {
            $catVidAge = Categories::leftJoin('category_details', function ($join) {
                $join->on('categories.id', '=', 'category_details.category_id');
                })->select(
                    'categories.id as id',
                    'category_details.name as name',
                    'categories.image as image',
                    'categories.status as status'
                )
                ->where('category_details.language_id', '=', $langId)
                ->where('categories.status', 1)
                ->with(['videoDetails' => function($query) use($langId, $start_age , $end_age){
                    $query->join('video_details', function ($joining) use($langId, $start_age , $end_age) {
                    $joining->on('videos.id', '=', 'video_details.video_id')->where('video_details.language_id', '=', $langId)->where('videos.start_age', '>=',$start_age)->where('videos.end_age', '<=',$end_age);    
                })->orderBy('videos.difficulty_id', 'ASC')->orderBy('videos.start_age', 'ASC')->orderBy('video_details.created_at', 'DESC')->limit(6);
                }])->get();
        }
        else
        {
            $whereArr = "";
            foreach($start_age_arr as $key=>$age){
                $whereArr .= "start_age >=".$age." and end_age <=".($age+1);
            }
            $catVidAge = Categories::leftJoin('category_details', function ($join) {
                $join->on('categories.id', '=', 'category_details.category_id');
                })->select(
                    'categories.id as id',
                    'category_details.name as name',
                    'categories.image as image',
                    'categories.status as status'
                )
                ->where('category_details.language_id', '=', $langId)
                ->where('categories.status', 1)
                ->where('categories.id', $cat_id)
                ->with(['videoDetails' => function($query) use($langId, $cat_id, $start_age , $end_age, $offset){
                    $query->join('video_details', function ($joining) use($langId, $cat_id,$start_age , $end_age, $offset) {
                        $joining->on('videos.id', '=', 'video_details.video_id')->where('video_details.language_id', '=', $langId)->where('videos.category_id', $cat_id)->where('videos.start_age', '>=',$start_age)->where('videos.end_age', '<=',$end_age);    
                    })->orderBy('videos.difficulty_id', 'ASC')->orderBy('videos.start_age', 'ASC')->orderBy('video_details.created_at', 'DESC')->skip($offset)->take(10);
            }])->get();
            // foreach($start_age_arr as $key=>$age){
                    
            //         $catVidAge_arr[$key] = Categories::leftJoin('category_details', function ($join) {
            //             $join->on('categories.id', '=', 'category_details.category_id');
            //             })->select(
            //                 'categories.id as id',
            //                 'category_details.name as name',
            //                 'categories.image as image',
            //                 'categories.status as status'
            //             )
            //             ->where('category_details.language_id', '=', $langId)
            //             ->where('categories.status', 1)
            //             ->where('categories.id', $cat_id)
            //             ->with(['videoDetails' => function($query) use($langId, $cat_id, $age ,$offset){
            //                 $query->join('video_details', function ($joining) use($langId, $cat_id,$age ,$offset) {
            //                     $joining->on('videos.id', '=', 'video_details.video_id')->where('video_details.language_id', '=', $langId)->where('videos.category_id', $cat_id)->where('videos.start_age', '>=',$age)->where('videos.end_age', '<=',$age+1);    
            //                 })->orderBy('videos.difficulty_id', 'ASC')->orderBy('videos.start_age', 'ASC')->orderBy('video_details.created_at', 'DESC')->skip($offset)->take(10);
            //         }])->toSql();
            //         dd($catVidAge_arr[$key]);
                   
            // }
           
        }            
        
                
        if($category && $catVidAge)
        {
            //dd(3);
            $output['video_age'] = $catVidAge;

            $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
            return response()->json($response);
        }
        else
        {
            //dd(4);
            $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> (object)[]);
            return response()->json($response);
        }
   }
   public function bothFilter($start_age,$end_age, $diff_id, $langId, $cat_id, $offset)
   {
        $output = array();
        $difficulty_id = $diff_id;
        $lang = Languages::find($langId);
        $lang_id = $lang->code;
        
        $folder = public_path('language/'.$lang_id.'.js');
        $json = file_get_contents($folder);
        $obj = json_decode($json, TRUE);
        

        $category = Categories::leftJoin('category_details', function ($join) {
            $join->on('categories.id', '=', 'category_details.category_id');
            })->select(
                    'categories.id as id',
                    'category_details.name as name',
                    'categories.image as image',
                    'categories.status as status'
                )
                ->where('category_details.language_id', '=', $langId)
                ->where('categories.status', 1)
                ->get();


        if($cat_id == 'all')
        {
            $catVidBoth = Categories::leftJoin('category_details', function ($join) {
                $join->on('categories.id', '=', 'category_details.category_id');
                })->select(
                    'categories.id as id',
                    'category_details.name as name',
                    'categories.image as image',
                    'categories.status as status'
                )
                ->where('category_details.language_id', '=', $langId)
                ->where('categories.status', 1)
                ->with(['videoDetails' => function($query) use($langId,$start_age,$end_age, $difficulty_id){
                    $query->join('video_details', function ($joining) use($langId,$start_age,$end_age, $difficulty_id) {
                        $joining->on('videos.id', '=', 'video_details.video_id')->where('video_details.language_id', '=', $langId)->where('videos.start_age', '>=',$start_age)->where('videos.end_age', '<=',$end_age)->where('videos.difficulty_id', $difficulty_id);    
                    })->orderBy('videos.difficulty_id', 'ASC')->orderBy('videos.start_age', 'ASC')->orderBy('video_details.created_at', 'DESC')->limit(6);
            }])->get();
        }   
        else
        {
            $catVidBoth = Categories::leftJoin('category_details', function ($join) {
                $join->on('categories.id', '=', 'category_details.category_id');
                })->select(
                    'categories.id as id',
                    'category_details.name as name',
                    'categories.image as image',
                    'categories.status as status'
                )
                ->where('category_details.language_id', '=', $langId)
                ->where('categories.status', 1)
                ->where('categories.id', $cat_id)
                ->with(['videoDetails' => function($query) use($langId, $cat_id, $start_age,$end_age, $difficulty_id, $offset){
                    $query->join('video_details', function ($joining) use($langId, $cat_id, $start_age,$end_age, $difficulty_id, $offset) {
                        $joining->on('videos.id', '=', 'video_details.video_id')->where('video_details.language_id', '=', $langId)->where('videos.category_id', $cat_id)->where('videos.start_age', '>=',$start_age)->where('videos.end_age', '<=',$end_age)->where('videos.difficulty_id', $difficulty_id);    
                    })->orderBy('videos.difficulty_id', 'ASC')->orderBy('videos.start_age', 'ASC')->orderBy('video_details.created_at', 'DESC')->skip($offset)->take(10);
            }])->get();
        }         
        
                
        if($category && $catVidBoth)
        {
            $output['video_age'] = $catVidBoth;

            $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
            return response()->json($response);
        }
        else
        {
            $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> []);
            return response()->json($response);
        }
   }


   public function getVideoDtails(Request $request)
   {
        $output = array();
        $inputs['header']['authorization'] = $request->header('Authorization');
        $inputs['header']['lang']          = $request->header('Accept-Language');
        $inputs['header']['accept']        = $request->header('Accept');
        $inputs['body']['video_id']        = $request->get('video_id');
        $inputs['body']['slug']            = $request->get('slug');
        $inputs['body']['offset']          = $request->get('offset');
        $offset                            = $inputs['body']['offset'];
        
        $folder                            = public_path('language/'.$inputs['header']['lang'].'.js');
        $json                              = file_get_contents($folder);
        $obj                               = json_decode($json, TRUE);
        $lang                              = Languages::where('code',$inputs['header']['lang'])->first();
        $langId                            = $lang->id; 
        if($inputs['body']['video_id'])
        {
            if($inputs['body']['slug'] == 'about' || $inputs['body']['slug'] == '')
            {
                $about = Videos::leftJoin('video_details', function ($join) {
                         $join->on('videos.id', '=', 'video_details.video_id');
                            })->select(
                                    'videos.id as id',
                                    'video_details.title as name',
                                    'video_details.about as about',
                                    'videos.status as status',
                                    'videos.category_id as category_id',
                                    'videos.start_age as start_age',
                                    'videos.end_age as end_age',
                                    'videos.image as image',
                                    'videos.video_file as video_file',
                                    'videos.difficulty_id as difficulty_id'
                                )
                                ->where('videos.id', '=', $inputs['body']['video_id'])
                                ->where('video_details.language_id', '=', $langId)
                                ->where('videos.status', 1)
                                ->first();
                $similar_videos = Videos::leftJoin('video_details',function ($join){
                         $join->on('videos.id', '=', 'video_details.video_id');
                            })->leftJoin('categories',function ($join){
                         $join->on('videos.category_id', '=', 'categories.id');
                            })
                ->select(
                                    'videos.id as id',
                                    'video_details.title as name',
                                    'videos.status as status',
                                    'videos.image as image',
                                    'videos.video_file as video_file',
                                    'videos.difficulty_id as difficulty_id',
                                    'videos.category_id as category_id',
                                     'videos.start_age as start_age',
                                     'videos.end_age as end_age',
                                    'categories.name as cat_name',
                                    'video_details.video_id as video_id'
                                )
                    ->where('video_details.language_id', '=', $langId)
                    ->where('videos.category_id', '=', $about['category_id'])
                    ->where('videos.start_age', '=', $about['start_age'])
                    ->where('videos.end_age', '=', $about['end_age'])
                    ->where('videos.difficulty_id', '=', $about['difficulty_id'])
                    ->where('video_details.video_id', '!=', $inputs['body']['video_id'])
                    ->where('videos.status', 1)
                    ->orderBy('video_details.created_at', 'DESC')
                    ->limit(6)
                    ->get();
                if(count($about) > 0 || count($similar_videos) > 0)
                {
                    $output['about'] = $about;
                    $output['similar_videos'] = $similar_videos;
                    $output['article'] = [];
                    $output['product'] = [];
                    $output['inputs'] = $inputs??(object)[];

                    $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
                    return response()->json($response);
                }
                else
                {
                    $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> (object)[]);
                    return response()->json($response);
                }
            }
            elseif($inputs['body']['slug'] == 'article' && ($offset || $offset == 0))
            {
                return $this->articleListing($inputs['body']['video_id'], $langId, $offset);
            }
            elseif($inputs['body']['slug'] == 'product' && ($offset || $offset == 0))
            {
                return $this->productListing($inputs['body']['video_id'], $langId, $offset);
            }
            else
            {
                $response = array('status'=>201, 'message'=>$obj['not_found'], 'result'=> (object)[]);
                return response()->json($response);
            }
        }
        else
        {
            $response = array('status'=>201, 'message'=>$obj['video_id_required'], 'result'=> (object)[]);
            return response()->json($response);   
        }
   }


   public function getSimilarVideo(Request $request)
   {
        $output = array();
        $inputs['header']['authorization'] = $request->header('Authorization');
        $inputs['header']['lang'] = $request->header('Accept-Language');
        $inputs['header']['accept'] = $request->header('Accept');
        $inputs['body']['video_id'] = $request->get('video_id');
        $inputs['body']['offset'] = $request->get('offset');
        $offset = $inputs['body']['offset'];

        $folder = public_path('language/'.$inputs['header']['lang'].'.js');
        $json = file_get_contents($folder);
        $obj = json_decode($json, TRUE);
        $lang = Languages::where('code',$inputs['header']['lang'])->first();
        $langId = $lang->id; 

        if($inputs['body']['video_id'])
        {
            
                $video = Videos::find($inputs['body']['video_id']);
                $similar_videos = Videos::leftJoin('video_details', function ($join) {
                         $join->on('videos.id', '=', 'video_details.video_id');
                            })->select(
                                    'videos.id as id',
                                    'video_details.title as name',
                                    'videos.status as status',
                                    'videos.image as image',
                                    'videos.video_file as video_file',
                                    'videos.difficulty_id as difficulty_id',
                                    'videos.category_id as category_id',
                                    'videos.start_age as start_age',
                                    'videos.end_age as end_age'
                                )
                                ->where('video_details.language_id', '=', $langId)
                                ->where('videos.category_id', '=', $video['category_id'])
                               ->where('videos.start_age', '=', $video['start_age'])
                               ->where('videos.end_age', '=', $video['end_age'])
                                ->where('videos.difficulty_id', '=', $video['difficulty_id'])
                                ->where('videos.status', 1)
                                ->orderBy('video_details.created_at', 'DESC')
                                ->skip($offset)
                                ->take(10)
                                ->get();

                if($similar_videos)
                {
                    $output['similar_videos'] = $similar_videos;
                    $output['inputs'] = $inputs??(object)[];

                    $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
                    return response()->json($response);
                }
        }
        else
        {
            $response = array('status'=>201, 'message'=>$obj['video_id_required'], 'result'=> (object)[]);
            return response()->json($response);   
        }
   }


   public function articleListing($id, $langId, $offset)
   {
        $output = array();
        $video_id = $id;
        $lang = Languages::find($langId);
        $lang_id = $lang->code;
        
        $folder = public_path('language/'.$lang_id.'.js');
        $json = file_get_contents($folder);
        $obj = json_decode($json, TRUE);

        $video_details = Videos::find($video_id);
        $cat_details   = Categories::find($video_details->category_id);

        // $article = Article_master::leftJoin('articles', function ($join) {
        //     $join->on('article_masters.id', '=', 'articles.master_id');
        //         })
        
        $article = Article_master::leftJoin('articles',function ($join){
            $join->on('article_masters.id', '=', 'articles.master_id');
               })->leftJoin('videos',function ($join){
            $join->on('article_masters.video_id', '=', 'videos.id');
               })->leftJoin('categories',function ($join){
                $join->on('categories.id', '=', 'videos.category_id');
                   })->select(
                            'article_masters.id as id',
                            'articles.title as title',
                            'articles.description as description',
                            'article_masters.image as image',
                            'article_masters.video as video',
                            'article_masters.status as status',
                            'categories.name as cat_name',
                            'categories.id as cat_id'
                        )
                        ->where('article_masters.video_id', '=', $video_id)
                        ->where('articles.lang_id', '=', $langId)
                        ->where('article_masters.status', 1)
                        ->orderBy('article_masters.created_at', 'DESC')
                        ->skip($offset)
                        ->take(10)
                        ->get();
        if(count( $article) > 0)
        {
            $output['about'] =(object)[];
            $output['similar_videos'] = [];
            $output['article'] = $article;
            $output['product'] = [];

            $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
            return response()->json($response);
        }
        else
        {
            $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> (object)[]);
            return response()->json($response);
        }
   }


   public function productListing($id, $langId, $offset)
   {
        $output = array();
        $video_id = $id;
        $lang = Languages::find($langId);
        $lang_id = $lang->code;
        
        $folder = public_path('language/'.$lang_id.'.js');
        $json = file_get_contents($folder);
        $obj = json_decode($json, TRUE);
        

        $product = Product_master::leftJoin('product_details', function ($join) {
                    $join->on('product_masters.id', '=', 'product_details.master_id');
                        })->select(
                            'product_masters.id as id',
                            'product_details.name as name',
                            'product_masters.image as image',
                            'product_masters.site_link as site_link',
                            'product_masters.price as price',
                            'product_masters.status as status'
                        )
                        ->where('product_masters.video_id', '=', $video_id)
                        ->where('product_details.language_id', '=', $langId)
                        ->where('product_masters.status', 0)
                        ->orderBy('product_masters.created_at', 'DESC')
                        ->skip($offset)
                        ->take(10)
                        ->get();    
        if(count($product) > 0)
        {
            $output['about'] = (object)[];
            $output['similar_videos'] = [];
            $output['article'] = [];
            $output['product'] = $product;
            $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
            return response()->json($response);
        }
        else
        {
            $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> (object)[]);
            return response()->json($response);
        }
   }


   public function savedVideo(Request $request)
   {
        $output = array();
        $inputs['header']['authorization'] = $request->header('Authorization');
        $inputs['header']['lang'] = $request->header('Accept-Language');
        $inputs['header']['accept'] = $request->header('Accept');
        $inputs['body']['video_id'] = $request->get('video_id');
        
        
        $folder = public_path('language/'.$inputs['header']['lang'].'.js');
        $json = file_get_contents($folder);
        $obj = json_decode($json, TRUE);
        
        $user =  auth('api')->user();

        if($inputs['body']['video_id'])
        {
            $exist_video = UserVideos::where('user_id', $user['id'])->where('video_id', $inputs['body']['video_id'])->where('type',2)->count();

            if($exist_video > 0)
            {
                $response = array('status'=>201, 'message'=>$obj['already-exist'], 'result'=> (object)[]);
                return response()->json($response);
            }
            else
            {
                $insert = UserVideos::insert([
                                'user_id' => $user['id'],
                                'video_id' => $inputs['body']['video_id'],
                                'type' => 2
                            ]); 
                
                if($insert) 
                {
                    $response = array('status'=>200, 'message'=>$obj['saved-successfully'], 'result'=> (object)[]);
                    return response()->json($response);
                } 
                else
                {
                    $response = array('status'=>201, 'message'=>$obj['saved-failed'], 'result'=> (object)[]);
                    return response()->json($response);
                }
            }
            
        }
        else
        {
            $response = array('status'=>201, 'message'=>$obj['required-field'], 'result'=> (object)[]);
            return response()->json($response);
        }
   }


   public function watchlistVideo(Request $request)
   {
        $output = array();
        $inputs['header']['authorization'] = $request->header('Authorization');
        $inputs['header']['lang'] = $request->header('Accept-Language');
        $inputs['header']['accept'] = $request->header('Accept');
        $inputs['body']['video_id'] = $request->get('video_id');
        
        
        $folder = public_path('language/'.$inputs['header']['lang'].'.js');
        $json = file_get_contents($folder);
        $obj = json_decode($json, TRUE);
        
        $user =  auth('api')->user();

        if($inputs['body']['video_id'])
        {
            $exist_video = UserVideos::where('user_id', $user['id'])->where('video_id', $inputs['body']['video_id'])->where('type', 1)->count();
            $video_details = Videos::where('id',$inputs['body']['video_id'])->first();
            //$category_details = Categories::('id',)
            if($exist_video > 0)
            {
                $delete = UserVideos::where('user_id', $user['id'])->where('video_id', $inputs['body']['video_id'])->where('type', 1)->delete();

                $insert = UserVideos::insert([
                                'user_id' => $user['id'],
                                'video_id' => $inputs['body']['video_id'],
                                'type' => 1
                            ]);
            }
            else
            {
                $insert = UserVideos::insert([
                                'user_id' => $user['id'],
                                'video_id' => $inputs['body']['video_id'],
                                'type' => 1
                            ]); 
            }

            if($insert) 
            {
                $response = array('status'=>200, 'message'=>$obj['watchlist-successfully'], 'result'=> (object)[]);
                return response()->json($response);
            } 
            else
            {
                $response = array('status'=>201, 'message'=>$obj['watchlist-failed'], 'result'=> (object)[]);
                return response()->json($response);
            }
            
        }
        else
        {
            $response = array('status'=>201, 'message'=>$obj['required-field'], 'result'=> (object)[]);
            return response()->json($response);
        }
   }


   public function savedFeedback(Request $request)
   {
        $output                             = array();
        $inputs['header']['authorization']  = $request->header('Authorization');
        $inputs['header']['lang']           = $request->header('Accept-Language');
        $inputs['header']['accept']         = $request->header('Accept');
        $inputs['body']['video_id']         = $request->get('video_id');
        $inputs['body']['feedback_details'] = $request->get('feedback_details');
        $inputs['body']['rating']           = $request->get('rating');
        
        $folder = public_path('language/'.$inputs['header']['lang'].'.js');
        $json = file_get_contents($folder);
        $obj = json_decode($json, TRUE);
        $user =  auth('api')->user();
        
        if($inputs['body']['video_id'] && $inputs['body']['feedback_details'] && $inputs['body']['rating'])
        {
           
                $insert = Feedback::insert([
                    'user_id'            => $user['id'],
                    'video_id'           => $inputs['body']['video_id'],
                    'rating'             => $inputs['body']['rating'],
                    'feedback_details'   => $inputs['body']['feedback_details'],
                    'status'             => 0
                 ]); 
                
                if($insert) 
                {
                    $response = array('status'=>200, 'message'=>$obj['feedback-successfull'], 'result'=> (object)[]);
                    return response()->json($response);
                } 
                else
                {
                    $response = array('status'=>201, 'message'=>$obj['feedback-failed'], 'result'=> (object)[]);
                    return response()->json($response);
                }  
        }
        else
        {
            $response = array('status'=>201, 'message'=>$obj['required-field'], 'result'=> (object)[]);
            return response()->json($response);
        }
   }


    public function getSubscriptionDetails(Request $request)
    {
        $output = array();
        $inputs['header']['authorization']  = $request->header('Authorization');
        $inputs['header']['lang']           = $request->header('Accept-Language');
        $inputs['header']['accept']         = $request->header('Accept');
        $inputs['body']['subcription_type'] = $request->get('subcription_type');
        $inputs['body']['offset']           = $request->get('offset');
        
        $folder                            = public_path('language/'.$inputs['header']['lang'].'.js');
        $json                              = file_get_contents($folder);
        $obj                               = json_decode($json, TRUE);
        $lang                              = Languages::where('code',$inputs['header']['lang'])->first();
        $langId                            = $lang->id; 

        $user =  auth('api')->user();
        if($user['membership_type'] == 3){
            $output['subscription_details'] = [];
            $response = array('status'=>200, 'message'=>'You are a VIP User', 'result'=> $output);

            //$response = array('status'=>200, 'message'=>'VIP User', 'result'=>[]);
            return response()->json($response);
        }else{
            if($inputs['body']['subcription_type'] == '')
            {
                        $get_all_subscription = Subscription::leftJoin('transactions',function ($join){
                        $join->on('subscriptions.id', '=', 'transactions.subscription_id');
                            })->leftJoin('packages',function ($join){
                        $join->on('subscriptions.pkg_id', '=', 'packages.id');
                        })->leftJoin('users',function ($join){
                        $join->on('subscriptions.user_id', '=', 'users.id');

                        })->select(
                            'subscriptions.id as id',
                            'subscriptions.subscription_start_date as start_date',
                            'subscriptions.subscription_end_date as end_date',
                            'subscriptions.status as status',
                            'transactions.trans_id as trans_id',
                            'transactions.trans_type as trans_status',
                            'packages.name as package_name',
                            'packages.duration as package_duration',
                            'packages.price as package_price'
                        )
                        ->where('subscriptions.user_id', $user['id'])
                        //->where('subscriptions.payment_status','=',1)
                        ->orderBy('subscriptions.created_at', 'DESC')->skip($inputs['body']['offset'] )->take(10)->get();
                        if(count($get_all_subscription) > 0)
                        {
                            $output['subscription_details'] = $get_all_subscription;
                            $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
                            return response()->json($response);
                        }
                        else
                        {
                            $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> (object)[]);
                            return response()->json($response);
                        }
            }
            else if($inputs['body']['subcription_type'] == 1)
            {
                        $get_all_subscription =Subscription::leftJoin('transactions',function ($join){
                        $join->on('subscriptions.id', '=', 'transactions.subscription_id');
                            })->leftJoin('packages',function ($join){
                        $join->on('subscriptions.pkg_id', '=', 'packages.id');
                        })->leftJoin('users',function ($join){
                        $join->on('subscriptions.user_id', '=', 'users.id');

                        })->select(
                            'subscriptions.id as id',
                            'subscriptions.subscription_start_date as start_date',
                            'subscriptions.subscription_end_date as end_date',
                            'subscriptions.status as status',
                            'transactions.trans_id as trans_id',
                            'transactions.trans_type as trans_status',
                            'packages.name as package_name',
                            'packages.duration as package_duration',
                            'packages.price as package_price'
                        )
                        ->where('subscriptions.user_id', $user['id'])
                        ->where('subscriptions.status', '=', 1)
                        //->where('subscriptions.payment_status','=',1)
                        ->orderBy('subscriptions.created_at', 'DESC')->skip($inputs['body']['offset'] )->take(10)->get();
                        if(count($get_all_subscription) > 0)
                        {
                            $output['subscription_details'] = $get_all_subscription;
                            $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
                            return response()->json($response);
                        }
                        else
                        {
                            $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> (object)[]);
                            return response()->json($response);
                        }
            }
            else if($inputs['body']['subcription_type'] == 2)
            {
                        $get_all_subscription =Subscription::leftJoin('transactions',function ($join){
                        $join->on('subscriptions.id', '=', 'transactions.subscription_id');
                            })->leftJoin('packages',function ($join){
                        $join->on('subscriptions.pkg_id', '=', 'packages.id');
                        })->leftJoin('users',function ($join){
                        $join->on('subscriptions.user_id', '=', 'users.id');

                        })->select(
                            'subscriptions.id as id',
                            'subscriptions.subscription_start_date as start_date',
                            'subscriptions.subscription_end_date as end_date',
                            'subscriptions.status as status',
                            'transactions.trans_id as trans_id',
                            'transactions.trans_type as trans_status',
                            'packages.name as package_name',
                            'packages.duration as package_duration',
                            'packages.price as package_price'
                        )
                        ->where('subscriptions.user_id', $user['id'])
                        ->where('subscriptions.status', '=', 2)
                       // ->where('subscriptions.payment_status','=',1)
                        ->orderBy('subscriptions.created_at', 'DESC')->skip($inputs['body']['offset'] )->take(10)->get();
                        if(count($get_all_subscription) > 0)
                        {
                            $output['subscription_details'] = $get_all_subscription;
                            $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
                            return response()->json($response);
                        }
                        else
                        {
                            $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> (object)[]);
                            return response()->json($response);
                        }
            }
            else if($inputs['body']['subcription_type'] == 3)
            {
                        $get_all_subscription =Subscription::leftJoin('transactions',function ($join){
                        $join->on('subscriptions.id', '=', 'transactions.subscription_id');
                            })->leftJoin('packages',function ($join){
                        $join->on('subscriptions.pkg_id', '=', 'packages.id');
                        })->leftJoin('users',function ($join){
                            $join->on('subscriptions.user_id', '=', 'users.id');
    
                        })
                        ->select(
                            'subscriptions.id as id',
                            'subscriptions.subscription_start_date as start_date',
                            'subscriptions.subscription_end_date as end_date',
                            'subscriptions.status as status',
                            'transactions.trans_id as trans_id',
                            'transactions.trans_type as trans_type',
                            'packages.name as package_name',
                            'packages.duration as package_duration',
                            'packages.price as package_price'
                            
                        )
                        ->where('subscriptions.user_id', $user['id'])
                        ->where('subscriptions.status', '=', 3)
                        //->where('subscriptions.payment_status','=',1)
                        ->orderBy('subscriptions.created_at', 'DESC')->skip($inputs['body']['offset'] )->take(10)->get();
                        if(count($get_all_subscription) > 0)
                        {
                            $output['subscription_details'] = $get_all_subscription;
                            $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
                            return response()->json($response);
                        }
                        else
                        {
                            $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> (object)[]);
                            return response()->json($response);
                        }
            }
            else
            {
                $response = array('status'=>201, 'message'=>$obj['not_found'], 'result'=> (object)[]);
                return response()->json($response);
            }
        }
    }

    public function userPayment(Request $request){
	    $output = array();
        $inputs['header']['authorization']  = $request->header('Authorization');
        $inputs['header']['lang']           = $request->header('Accept-Language');
        $inputs['header']['accept']         = $request->header('Accept');
        $inputs['body']['pkg_id']           = $request->get('pkg_id');
        $inputs['body']['amount']           = $request->get('amount');

        $folder                            = public_path('language/'.$inputs['header']['lang'].'.js');
        $json                              = file_get_contents($folder);
        $obj                               = json_decode($json, TRUE);
        $lang                              = Languages::where('code',$inputs['header']['lang'])->first();
        $langId                            = $lang->id; 
        $user                              = auth('api')->user();

        if($inputs['body']['amount'] &&  $inputs['body']['pkg_id'])
        {


             $data = [];
             $data['items'] = [
                    [
                      'name' => "pkgid#". $inputs['body']['pkg_id'] ,//package id send to paypal
                      'price' => $inputs['body']['amount'],
                     'desc'  => "userid#".$user['id'] //user id send to paypal
                     //'qty' => $inputs['body']['amount'] 
                    ]
                ];
                 
            $data['invoice_id'] = mt_rand(100000,999999);
            $data['invoice_description'] = "Package Subscription";
            $data['return_url'] = URL::to('/paypal-payment-success');
            $data['cancel_url'] = URL::to('/paypal-payment-cancel');
            $data['total'] = $inputs['body']['amount'];
            $provider = new ExpressCheckout;
            $paypal_response = $provider->setExpressCheckout($data);
            $paypal_response = $provider->setExpressCheckout($data, true);
            if($paypal_response['ACK']=='Success'){
                $output['subscription_details'] = $paypal_response;
                $response = array('status'=>200, 'message'=>$obj['payment-succeess'], 'result'=> $output);
                return response()->json($response);

              
            }else{
                $output['subscription_details'] = '';
                $response = array('status'=>201, 'message'=>$obj['payment-failed'], 'result'=> $output);
                return response()->json($response);
            }
        }
        else
        {

            $response = array('status'=>201, 'message'=>$obj['not_found'], 'result'=> (object)[]);
            return response()->json($response);
        }
    }

    public function getCmsDetails(Request $request)
    {
        
        $output = array();
        $inputs['header']['authorization']  = $request->header('Authorization');
        $inputs['header']['lang']           = $request->header('Accept-Language');
        $inputs['header']['accept']         = $request->header('Accept');
        $inputs['body']['cms_id']           = $request->get('cms_id');
        
        $folder                            = public_path('language/'.$inputs['header']['lang'].'.js');
        $json                              = file_get_contents($folder);
        $obj                               = json_decode($json, TRUE);
        $lang                              = Languages::where('code',$inputs['header']['lang'])->first();
        $langId                            = $lang->id; 

        $get_cms_details  = Master_cms::leftJoin('cms',function ($join){
            $join->on('master_cms.id', '=', 'cms.master_id');
               })->select(
                            'cms.title as title',
                            'cms.description as description'

                        )
                        ->where('cms.master_id', '=', $inputs['body']['cms_id'] )
                        ->where('cms.lang_id', '=', $langId)
                        ->first();
        if(count( $get_cms_details) > 0)
        {
            $output['cms'] = $get_cms_details;

            $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
            return response()->json($response);
        }
        else
        {
            $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> (object)[]);
            return response()->json($response);
        }
    }
    
    public function getEmailNotification(Request $request)
    {
        $output = array();
        $inputs['header']['authorization']  = $request->header('Authorization');
        $inputs['header']['lang']           = $request->header('Accept-Language');
        $inputs['header']['accept']         = $request->header('Accept');
        $inputs['body']['email_notify']     = $request->get('email_notify');
        
        $folder                             = public_path('language/'.$inputs['header']['lang'].'.js');
        $json                               = file_get_contents($folder);
        $obj                                = json_decode($json, TRUE);
        $lang                               = Languages::where('code',$inputs['header']['lang'])->first();
        $langId                             = $lang->id;
        $user                               = auth('api')->user();
        $get                                = Notification_management::where('user_id', $user['id'])->get();
        if(count($get)>0){
            $to_update = array(
                            'email_notify' => $inputs['body']['email_notify']
            );
            $insert = Notification_management::where('user_id', $user['id'])->update($to_update);
        }
        else{
            $to_insert = array(
                            'user_id' => $user['id'],
                            'email_notify' => $inputs['body']['email_notify']
                        );
            $insert =  Notification_management::insert($to_insert);
        }
        if($insert)
        {
            $response = array('status'=>200, 'message'=>"Success");
            return response()->json($response);
        }
        else
        {
            $response = array('status'=>201, 'message'=>"Failed");
            return response()->json($response);
        }
    }

    public function getNotificationStatus(Request $request)
    {
        $output = array();
        $inputs['header']['authorization']  = $request->header('Authorization');
        $inputs['header']['lang']           = $request->header('Accept-Language');
        $inputs['header']['accept']         = $request->header('Accept');
        
        $folder                             = public_path('language/'.$inputs['header']['lang'].'.js');
        $json                               = file_get_contents($folder);
        $obj                                = json_decode($json, TRUE);
        $lang                               = Languages::where('code',$inputs['header']['lang'])->first();
        $langId                             = $lang->id;
        $user                               = auth('api')->user();
        $get                                = Notification_management::where('user_id', $user['id'])->first();

        

        if(count($get)>0 && $get->email_notify == 1){
            $status = 1;
        }
        else if(count($get)>0 && $get->email_notify == 0){
            $status = 0;
        }
        else{
            $status = 0;
        }

        if($status == 1 || $status == 0)
        {
            $output['status'] = $status;
            $response = array('status'=>200, 'message'=>'Success', 'result'=> $output);
            return response()->json($response);
        }
        else
        {
            $response = array('status'=>201, 'message'=>'Failed', 'result'=> []);
            return response()->json($response);
        }

    }

    public function cancelSubscription(Request $request)
    {
        $output = array();
        $inputs['header']['authorization']  = $request->header('Authorization');
        $inputs['header']['lang']           = $request->header('Accept-Language');
        $inputs['header']['accept']         = $request->header('Accept');
        $inputs['body']['order_id']         = $request->get('order_id');
        
        $folder                             = public_path('language/'.$inputs['header']['lang'].'.js');
        $json                               = file_get_contents($folder);
        $obj                                = json_decode($json, TRUE);
        $lang                               = Languages::where('code',$inputs['header']['lang'])->first();
        $langId                             = $lang->id;
        $user                               = auth('api')->user();
        $get_subscription_data              = Subscription::where('id', $inputs['body']['order_id'])->first();
        $curr_date                          = date("Y-m-d H:i:s");

        if(count($get_subscription_data)>0){
            $user_update = array(
                'package_id'       => NULL,
                'curr_pkg_end_dt'  => NULL,
                'membership_type'  =>  1
            );
            $subscription_update = array(
                'status'                   => 3,
                'subscription_cancel_date' => $curr_date
            );
        }
        //dd($user['id']);

        $user_update = User::where('id', $user['id'])->update($user_update);
        $subscrition_update = Subscription::where('id',  $inputs['body']['order_id'] )->update($subscription_update);
        $updated_subs_data    = Subscription::where('id', $inputs['body']['order_id'])->first();
        if($subscrition_update)
        {
            $output['order_data'] = $updated_subs_data;
            $response = array('status'=>200, 'message'=>'Success', 'result'=> $output);
            return response()->json($response);
        }
        else
        {
            $response = array('status'=>201, 'message'=>'Failed', 'result'=> []);
            return response()->json($response);
        }


    }

    public function getguideDetails(Request $request)
    {
        $output = array();
        $inputs['header']['authorization']  = $request->header('Authorization');
        $inputs['header']['lang']           = $request->header('Accept-Language');
        $inputs['header']['accept']         = $request->header('Accept');
        $inputs['body']['video_id']         = $request->get('video_id');
        $video_id                           = $inputs['body']['video_id'];
        
        $folder                            = public_path('language/'.$inputs['header']['lang'].'.js');
        $json                              = file_get_contents($folder);
        $obj                               = json_decode($json, TRUE);
        $lang                              = Languages::where('code',$inputs['header']['lang'])->first();
        $langId                            = $lang->id;
                
        $guide = Guide_master::leftJoin('guides', function ($join) {
                $join->on('guide_masters.id', '=', 'guides.master_id');
                 })->leftJoin('videos',function ($join){
                 $join->on('guide_masters.video_id', '=', 'videos.id');
                 })->leftJoin('categories',function ($join){
                 $join->on('categories.id', '=', 'videos.category_id');
                 })
                    ->select(
                        'guides.title as title',
                        'guides.description as description',
                        'guide_masters.image as image',
                        'categories.name as cat_name',
                        'categories.image as cat_icon'
                    )->where('guide_masters.video_id', '=', $video_id)->where('guides.lang_id', '=', $langId)->get();
        foreach($guide as $key=>$val){
            $val['cat'] = url('storage/category').'/'.$val['cat_icon'];
        }          
        if(count( $guide) > 0)
        {
            $output['guide'] = $guide;
            $response = array('status'=>200, 'message'=>$obj['list-fetch-success'], 'result'=> $output);
            return response()->json($response);
        }
        else
        {
            $response = array('status'=>201, 'message'=>$obj['list-fetch-failed'], 'result'=> (object)[]);
            return response()->json($response);
        }
    }



   
}
