<?php

// ini_set("memory_limit", "-1");
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);

require_once "/home/mysamm/Lib/MysqlDB/connection.php"; 
require_once "/home/mysamm/Lib/MongoDB/mongo_connect.php"; 
require_once "/home/mysamm/public_html/vendor/autoload.php"; 
ini_set('max_execution_time', 500);

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class ApiSdata extends Connection{
	function __construct() {
		$this->mdb = new MongoConn();
		$this->db = new connection();
	}
	
	function get_assigned_category($REQUEST){
		
		$collections = $REQUEST['tbl'];
		$report_id = $REQUEST['request'];
		$filter = array('report_id'=>$report_id);
		$data = $this->mdb->Query($collections,$filter);
		foreach($data as $row){
			$rowm = (array) $row;
			unset($rowm['_id']);
			$return[] = $rowm;
		}
		$data_json  = $this->encodeJson($return);
		echo $data_json; exit;
	
	}
	
	function get_assigned_category_dev($REQUEST){
		
		$collections = $REQUEST['tbl'];
		$report_id = $REQUEST['request'];
		$website_id = $REQUEST['website'];
		$filter = array('report_id'=>$report_id,'website_id'=>$website_id);
		$data = $this->mdb->Query($collections,$filter);
		foreach($data as $row){
			$rowm = (array) $row;
			unset($rowm['_id']);
			$return[] = $rowm;
		}
		$data_json  = $this->encodeJson($return);
		echo $data_json; exit;
	
	}
	
	
	
	
	
	
	
	function check_category_is_running_status($REQUEST){ 
			$report_id = $REQUEST['request'];
			$user_id = $REQUEST['user_id'];
			$website_id = $REQUEST['website_id'];
			$running_instnances = shell_exec("ps aux|grep 'website_category_assign.php $user_id $report_id $website_id' | grep -v 'grep' |wc -l");
			$return["status"] = trim($running_instnances);
			$data_json  = $this->encodeJson($return);
			echo $data_json; exit;
	}
	
	
	
	
	function discards_changes($REQUEST){
		
		$collections = $REQUEST['tbl'];
		$report_id = $REQUEST['request'];
		$filter = array('report_id'=>$report_id);
	    $this->mdb->delete_document($collections,$filter);
		$return[] = '1';
		$data_json  = $this->encodeJson($return);
		echo $data_json; exit;
	}
	
	
	function discards_changes_dev($REQUEST){
		
		$collections = $REQUEST['tbl'];
		$report_id = $REQUEST['request'];
		$filter = array('report_id'=>$report_id);
	    $this->mdb->delete_document($collections,$filter);
		$return[] = '1';
		$data_json  = $this->encodeJson($return);
		echo $data_json; exit;
	}
	
	
	function run_category_assignment($REQUEST){
		
		$report_id = $REQUEST['request'];
		$website_id = $REQUEST['website_id'];
		$user_id = $REQUEST['user_id'];
		
		$cmd="nohup php /home/mysamm/openai/ContentGenerator/website_category_assign.php $user_id $website_id $report_id >/dev/null 2>&1 & ";
		shell_exec($cmd);
		
		$return[] = '1';
		$data_json  = $this->encodeJson($return);
		echo $data_json; exit;
	}
	
	
	function run_category_assignment_dev($REQUEST){
		
		$report_id = $REQUEST['request'];
		$website_id = $REQUEST['website_id'];
		$user_id = $REQUEST['user_id'];
		
		$website_array = explode(',', $website_id);
		foreach($website_array as $web_id){
			$cmd = "nohup php /home/mysamm/openai/ContentGenerator/website_category_assign_dev.php $user_id $web_id $report_id >/dev/null 2>&1 & ";
			shell_exec($cmd); 
		}
		
		$return[] = '1';
		$data_json  = $this->encodeJson($return);
		echo $data_json; exit;
	}
	
	function check_api_key($REQUEST){
		
		$api_key = $REQUEST['api_key'];
		$final_cmd = "gptsparam output:'product_name' sku:'82064BK' product_name:'Medium Wall Mount Lantern' product_type:'OUTDOOR' attr_finish:'Black' attr_dimension:'18.5x7.5x6.5' brand:'LARK' name_max_word_limit:'15' name_avg_char:'81.72' sample_name_term:'Size Present,machine washable,indoor/outdoor,chantille rug,traditional,floral,area rug,waterproof,patio,rug,contemporary,abstract,tropical,geo,striped,pattern,beige,multi,black,green' avg_bullet_point:'9.59' max_word_bullet:'10' sample_bullet_term:'pile,outdoor,easy,rug,height,stain,durable,fiber,area,maintenance,weather,room,safe,pet-friendly,hose,air,uv-resistant,trendy,cleaning,water', min_word_romance_copy:'0' max_word_romance_copy:'0' additional_feilds:'{}' do_not_include_item_number:'1' do_not_include_brand:'1' is_white_labeled:'0' is_include_size:'1' remove_punctuation:'0' apikey:'{$api_key}' additional_product_name_rule:'' additional_bullet_point_rule:'' additional_romance_copy_rules:''";
		
		$data = shell_exec($final_cmd);	

		
		$date_json_decoded = array();
		
		$data_Arr = explode("####", $data);
		$json1 = trim($data_Arr[0]);
		$date_json_decoded = json_decode($json1, true);
		

		if(!empty($date_json_decoded)){ 
			return 1;
		}else{
			return $data;
		} 
	}
	
	
	function save_category_as_prim($REQUEST){ 
			
		$report_id = $REQUEST['request'];
		$category_id = $REQUEST['category_id'];
		$tbl = $REQUEST['tbl'];
		$user_id = $REQUEST['user_id'];
		$save_key = base64_decode($REQUEST['save_key']);
		$exploded = explode("**", $save_key);
		$website_id = $exploded['0'];
		$sku  = $exploded['1'];
		$filter = array('report_id'=>$report_id,'website_id'=>$website_id,'sku'=>$sku);
		$update = array('$set'=>array('is_prime'=>(int) $category_id));
		$this->mdb->updateMany($tbl, $filter, $update);
			
	}
	
	function compareDates($date1, $date2){
      if (strtotime($date1) < strtotime($date2))
         return 1;
      else if (strtotime($date1) > strtotime($date2))
         return -1;
      else
         return 0;
	}
	
	
	function match_string($string_1, $string_2){
		
		
		$string_1 = strtolower($string_1);
		$string_1 = trim($string_1);
		$string_1 = preg_replace("/\W+/is", "", $string_1);
		
		
		$string_2 = strtolower($string_2);
		$string_2 = trim($string_2);
		$string_2 = preg_replace("/\W+/is", "", $string_2);
		
		if($string_1 == $string_2){
			return 1;
		}else{
			return 2;
		}
	}
	
	public function encodeJson($responseData) {
		$jsonResponse = json_encode($responseData, JSON_INVALID_UTF8_IGNORE);
		return $jsonResponse;		
	}
  

}

?>