<?php
	namespace App\Http\Controllers\v2019;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Auth\authentication;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Action\Common;
 
class Insights1Controller extends Controller
{ 
	public $appType = 'SM' ;
    public function __construct(Request $request) {
        if($request->has('debug') && $request->input('debug') == true) {
            $this->debug = true;			
        }
		if($request->has('appType') && $request->input('appType') == true) {
            $this->appType = $request->input('appType');		
        }
    }
	
	public function getInsights(Request $request)
    {      
		$resourceType = "app";
		$source = "listing";
		$result = array();	
		if($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		if($request->has('source'))
			$source = strtolower($request->input('source'));
		
		$filters = array();
		if($request->has('filters') && !empty($request->input('filters')))
		    $filters = $request->input('filters');
		
		if($resourceType=='web'){
			$code = 1001;				
			$message = "Success";
			$offset=0;
			$pageValue=1;
			if ($request->has('pageValue') && $request->input('pageValue')!='')
					$pageValue = $request->input('pageValue');	
				
			if ($request->has('memid') && $request->input('memid')!=''){								
				$offset = ($pageValue-1)* DEFAULT_NEWS_LIMIT;
				
				$memid = $request->input('memid');
				$auth = new authentication();
				$user_id = $auth->authUserId($memid);//for web user
				//print_r($filters);
				if ($user_id) {
					//run query here
					if($pageValue>1 || count($filters)>0){			
						$result = \ApiQuery::getInsightsRows($user_id, $source, $resourceType, $type='', $filters,'post',DEFAULT_NEWS_LIMIT, $offset,$this->appType);
						/* $sql = DB::table('tbl_news_post')->offset($offset)->limit(DEFAULT_LIMIT); 
						$result = $sql->get(); */
					}else {/// data from json array file 
					
						$result = \ApiQuery::getInsightsRows($user_id, $source, $resourceType, $type='default', $filters,'post',DEFAULT_NEWS_LIMIT, $offset,$this->appType);
						////$sql = DB::table('tbl_news_post')->limit(DEFAULT_NEWS_LIMIT); 
						///include_once "/home/steelmin/public_html/api/storage/v2019/json/web/insight/listing_post.php";
					}
				}else{
					$code = 2013;
					$message = "Unauthorized action.";
				}
			}else{/// pre data from json array file 
				////$sql = DB::table('tbl_news_post')->limit(5);
				$result = \ApiQuery::getInsightsRows($memid=0, $source, $resourceType, $type='default', $filters,'pre',DEFAULT_NEWS_LIMIT, $offset,$this->appType);
				//include_once "/home/steelmin/public_html/api/storage/v2019/json/web/insight/listing_pre.php";
			}
		}else{
			///include('/home/steelmin/public_html/api/storage/v2019/insight_pre.php');
			if ($request->has('accessToken') && $request->input('accessToken')!='') {
			   // $auth = new authentication();
					$auth = new authentication();	
					$token = $request->input('accessToken');
					$memid = $auth->authToken($token);	
					
					$pageValue = $request->input('pageValue');					
					$offset = ($pageValue-1)* DEFAULT_NEWS_LIMIT;
					
					if ($memid) {
						if($pageValue>1 || count($filters)>0){			
							$result = \ApiQuery::getInsightsRows($memid, $source, $resourceType, $type='', $filters,'post',DEFAULT_NEWS_LIMIT, $offset,$this->appType);
						}else {/// data from json array file 
							$result = \ApiQuery::getInsightsRows($memid, $source, $resourceType, $type='default', $filters,'post',DEFAULT_NEWS_LIMIT, $offset,$this->appType);
							////$sql = DB::table('intel')->limit(DEFAULT_NEWS_LIMIT); 
							//include_once "/home/steelmin/public_html/api/storage/v2019/json/app/intel/intel_app_post.php";
						}
						///include('/home/steelmin/public_html/api/storage/v2019/insight_post.php');
						$code = 1001;
						$message = "Success";
					}else{
						$code = 2013;
						$message = "Unauthorized action.";
					}
			}else{
				///not loggedin/visitor///
				$result = \ApiQuery::getInsightsRows($memid=0, $source, $resourceType, $type='default', $filters,'pre',DEFAULT_NEWS_LIMIT, $offset,$this->appType);
					//include_once "/home/steelmin/public_html/api/storage/v2019/json/app/intel/intel_app_pre.php";
				$code = 1001;
				$message = "Success";
			}
		}
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    } 
	
	public function getInsightDetails(Request $request)
    {      
		$resourceType = "app";
		$filters = array();
		if($request->has('filters') && !empty($request->input('filters')))
		    $filters = $request->input('filters');
		$result = array();	
		if($request->has('resourceType'))
			$resourceType = strtolower($request->input('resourceType'));
		
		if($request->has('itemID') && !empty($request->input('itemID'))){
		    $itemID = $request->input('itemID');
			$filters['itemID'] = $itemID;
		}else{
			$code = 2013;
			$message = "Invalid! itemID missing.";
			
			return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
		}
		
		if($resourceType=='app' && $request->has('accessToken') && !empty($request->input('accessToken'))){
		    $auth = new authentication();	
			$token = $request->input('accessToken');
			$memid = $auth->authToken($token);	
		}elseif($resourceType=='web' && $request->has('memid') && !empty($request->input('memid'))){
			$memid = $request->input('memid');
			$auth = new authentication();
			$memid = $auth->authUserId($memid);//for web user
		}	
		
		
		if($memid){
			///full data///
			//include('/home/steelmin/public_html/api/storage/v2019/insight_details.php');
			
			$resultDetails = \ApiQuery::getInsightsRows($memid, $source='details', $resourceType, $type='', $filters, 'post');
			if(count($resultDetails)>0){
				$result = $resultDetails[0];
			}
			$code = 1001;
			$message = "Success";
			
		}else{
			///not loggedin/visitor///
			
			$resultDetails = \ApiQuery::getInsightsRows($memid=0, $source='details', $resourceType, $type='', $filters, 'pre');
			if(count($resultDetails)>0){
				$result = $resultDetails[0];
			}
			$code = 1001;
			$message = "Success";
		}
		
	   return new JsonResponse(array("status"=>array("code" => $code, "message" => $message), "data"=> $result));
    }
	
	public function updateMostread(Request $request)
    {
		$resourceType = "app";
		
		if($request->has('resourceType'))
			$resourceType = strtolower($request->input('source'));
		
		if($request->has('itemId') && !empty($request->input('itemId'))){
		    $itemId = $request->input('itemId');
		
			if($resourceType=='web'){
				if($request->has('memid')){
					$memid = $request->input('memid');
					$memid = _encrypt_decrypt($action='decrypt', $memid);
					$auth = new authentication();
					$user_id = $auth->authUserId($memid);//for web user
					if ($user_id) {
						$post_id = 0;
						$count = 1;

						if($request->has('itemId')) {
							$post_id = $request->input('itemId');
						}

						if($request->has('count') && (int)$request->input('count') > 0) {
							$count = $request->input('count');
						}

						$sql = "UPDATE `tbl_news_post` SET `most_read` = (`most_read` + " . (int)$count . ") WHERE `post_id` = " . (int)$post_id;

						DB::statement($sql);
						$code = 1001;
						$message = "Updated successfully";
					}else{
						$code = 2013;
						$message = "Unauthorized action.";
					}
				}else{
					$code = 2013;
					$message = "Unauthorized action.";
				}
			}else{	
				if ($request->has('accessToken') && $request->input('accessToken')!='') {
				   // $auth = new authentication();
				    $token = $request->input('accessToken');
					$auth = new authentication();
					$token = $request->input('token');
					$user_id = $auth->authToken($token);
					if ($user_id) {
						$us = new User($user_id);
						$userAccess = $us->getTakenServicesByUser();
					} else {
						 return new JsonResponse(array(
							"code" => 2013, "message" => "Unauthorized action. Token access denied"
						)) ;
					}

					$post_id = 0;
					$count = 1;

					if($request->has('itemId')) {
						$post_id = $request->input('itemId');
					}

					if($request->has('count') && (int)$request->input('count') > 0) {
						$count = $request->input('count');
					}

					$sql = "UPDATE `tbl_news_post` SET `most_read` = (`most_read` + " . (int)$count . ") WHERE `post_id` = " . (int)$post_id;

					DB::statement($sql);
					$code = 1001;
					$message = "Updated successfully";

				}else{
					$code = 2013;
					$message = "Unauthorized action.";
				}
			}
		}else{
			$code = 2013;
			$message = "Invalid not found.";
		}
		return new JsonResponse(array("status"=>array("code" => $code, "message" => $message)));
    }
}