<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>

<?php

// setting up asin payload
$asin_payload 	= ''; 
if(count($product_batch)==0) return;
$asin_payload = implode( "," , $product_batch);
$asin_payload = wp_json_encode($product_batch);

$country		=  json_decode( sanitize_text_field( get_option('amazingaffiliates_settings_api_country') ) );
		
if($is_autoupdate) update_option( 'amazingaffiliates_settings_product_pages_auto_update_lastasinpayload', $asin_payload );
$update_status = 'Error';

// setting up API payload
$partner_tag 	= sanitize_text_field( get_option('amazingaffiliates_settings_api_partner_tag') );
$partner_type 	= "Associates";
$marketplace	= $country->amazon_domain;
$payload =	"{"
            ." \"ItemIds\": " . $asin_payload . ","
            ." \"Resources\": ["
            ."  \"ItemInfo.Features\","
            ."  \"Images.Primary.Medium\","
            ."  \"Images.Primary.Large\","
            ."  \"ItemInfo.Title\","
            ."  \"ItemInfo.ByLineInfo\","
            ."  \"ItemInfo.ManufactureInfo\","
            ."  \"Offers.Listings.Availability.Message\","
            ."  \"Offers.Listings.Availability.Type\","
			."	\"Offers.Listings.Condition\","
            ."  \"Offers.Listings.DeliveryInfo.IsAmazonFulfilled\","
			."	\"Offers.Listings.MerchantInfo\","
            ."  \"Offers.Listings.Price\","
            ."  \"Offers.Listings.SavingBasis\","
			."  \"OffersV2.Listings.Availability\","
			."  \"OffersV2.Listings.Condition\","
			."  \"OffersV2.Listings.MerchantInfo\","
			."  \"OffersV2.Listings.Price\","
			."  \"OffersV2.Listings.Type\""
            ." ],"
            ." \"PartnerTag\": \"" . $partner_tag  . "\","
            ." \"PartnerType\": \"" . $partner_type  . "\","
            ." \"Marketplace\": \"" . $marketplace  . "\""
            ."}";

// setting up class AwsV4 variables 
$serviceName = "ProductAdvertisingAPI";
$region     = $country->region;
$accessKey  = sanitize_text_field( get_option('amazingaffiliates_settings_api_accessKey') );
$secretKey  = sanitize_text_field( get_option('amazingaffiliates_settings_api_secretKey') );
$host       = $country->pa_endpoint;
$uriPath    = "/paapi5/getitems";

// initializing class AwsV4
require_once( plugin_dir_path( __FILE__ ) . 'PAAPI5_class_AwsV4.php' );

$awsv4 = new AmazingAffiliates_custom_AwsV4 ($accessKey, $secretKey);
$awsv4->setRegionName($region);
$awsv4->setServiceName($serviceName);
$awsv4->setPath ($uriPath);
$awsv4->setPayload ($payload);
$awsv4->setRequestMethod ("POST");
$awsv4->addHeader ('content-encoding', 'amz-1.0');
$awsv4->addHeader ('content-type', 'application/json; charset=utf-8');
$awsv4->addHeader ('host', $host);
$awsv4->addHeader ('x-amz-target', 'com.amazon.paapi5.v1.ProductAdvertisingAPIv1.GetItems');
$headers = $awsv4->getHeaders ();
$headerString = "";
foreach ( $headers as $key => $value ) {
	$headerString .= $key . ': ' . $value . "\r\n";
} 
$params = array (
    'http' => array (
        'header' => $headerString,
		'method' => 'POST',
		'content' => $payload
	)
); 
$stream = stream_context_create( $params ); 

if($is_autoupdate) update_option( 'amazingaffiliates_settings_product_pages_auto_update_lastupdatestatus', 'there was a problem while connecting to the APIs' );
$full_response = wp_remote_post(
	'https://'.$host.$uriPath, array(
		'headers' => $headers,
		'method' => 'POST',
		'body' => $payload
));
$http_response =  (array) $full_response['http_response'];
$prefix = chr( 0 ) . '*' . chr( 0 );
$response = $http_response[ $prefix . 'response' ]->body;
