<?php

session_start();
//include_once 'inc/config.php';
require_once __DIR__ . '/vendor/autoload.php';
include_once 'inc/functions.php';
include 'inc/database.php';
use Bigcommerce\Api\Client as Bigcommerce;

if( isset($_GET['payload']) || (isset($_GET['store_hash']) && !empty($_GET['store_hash'])) ){
	$data = decodeSignedRequest($_GET['payload']);
	$hash = isset($data['store_hash']) ? $data['store_hash'] : $_GET[
		'store_hash'];
		//echo "hash ".$hash;exit();
	$access_token = $db->getSingleColumn( "stores", "access_token", array( "store_id" => $hash ));
	
    $product_Id	= $_POST['id'];
    $store_hash	= $hash;
    $token		= $access_token;
    $config 	= array(
                'client_id'  => CLIENT_ID,
                'auth_token' => $token,
                'store_hash' => $store_hash
    );
   // print_r($config); exit();
    Bigcommerce::configure($config);
    Bigcommerce::verifyPeer(false);
    
    
    $limit	= 250;
    $page 	= 1;
    $customer_products=array();
    for($i=1;$i<=$page;$i++){
    	
    	$curl = curl_init();
    	curl_setopt_array($curl, array(
    	  CURLOPT_URL => "https://api.bigcommerce.com/stores/".$store_hash."/v3/wishlists?page=".$page."&limit=".$limit,
    	  CURLOPT_RETURNTRANSFER => true,
    	  CURLOPT_ENCODING => "",
    	  CURLOPT_MAXREDIRS => 10,
    	  CURLOPT_TIMEOUT => 30,
    	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    	  CURLOPT_CUSTOMREQUEST => "GET",
    	  CURLOPT_HTTPHEADER => array(
    		"Accept: application/json",
    		"Cache-Control: no-cache",
    		"Connection: keep-alive",
    		"Content-Type: application/json",
    		"X-Auth-Client: ".CLIENT_ID,
    		"X-Auth-Token: ".$token,
    		"cache-control: no-cache"
    	  ),
    	));
    	$response = curl_exec($curl);
    	$err = curl_error($curl);
    	curl_close($curl);
    	
    	if($response){
    		
    		$response=json_decode($response,true);
    		$wishlists=$response['data'];
    		$count=$response['meta']['pagination']['count'];
    		if($count){
    			//$total_count	= $total_count + $count;
    			foreach($wishlists as $key=>$singleWishlist){
    			$id				= $singleWishlist['id'];
    			$customer_id	= $singleWishlist['customer_id'];	
    			$items			= $singleWishlist['items'];
    				if(!empty($items)) {
    					foreach($items as $key=>$item){	
    						$customer_products[$customer_id][]	= $item['product_id'];
    						
    					}	
    				}
    			}
    			$page++;
    		}
    	}
    
    }
    $customer_data=array();
    $j=0;
    
    if($customer_products){
    	$customer_data[$j][]	= "Product Id";
    	$customer_data[$j][]	= "Customer Id";
    	$customer_data[$j][]	= "Customer Name";
    	$customer_data[$j][]	= "Email";
    	foreach($customer_products as $keys=>$customer_product){
    		if(in_array($product_Id, $customer_product)){
    			$j++;
    			$customers	= Bigcommerce::getCustomer($keys);
    			
    			$customer_id	= $customers->id;
    			$first_name		= $customers->first_name;
    			$last_name		= $customers->last_name;
    			$email			= $customers->email;
    			
    			$customer_data[$j][]	= $product_Id;
    			$customer_data[$j][]	= $customer_id;
    			$customer_data[$j][]	= $first_name." ".$last_name;
    			$customer_data[$j][]	= $email;
    			
    		}		
    	}
    }
    
    die( json_encode($customer_data) );

}
?>