<?php
set_time_limit(0);
ini_set('memory_limit', '-1');
require_once("includes/config.php");

require_once("includes/etsyv3/vendor/autoload.php");
use OAuth\Client;
$user_id = 2508;
$api_key = $etsyConfig['api_key'];
	$api_secret = $etsyConfig['api_secret'];    
   
    $etsyResult = $conn->query("SELECT * FROM `etsy_keys` where user_id = ".$user_id);
    if($etsyResult->num_rows == 0) {
		return "No valid Etsy keys found!";
	}
    $etsyRow = $etsyResult->fetch_assoc();     
    $oauth_token = $etsyRow['oauth_token'];
    $oauth_token_secret = $etsyRow['oauth_token_secret'];
    $access_token = $etsyRow['access_token'];
	$refresh_token = $etsyRow['refresh_token']; 
    $shop_id = $etsyRow['shop_id'];
    $listing_id = 1580816733;
    $inv = '{"products":[{"sku":"41505819689157","property_values":[{"property_id":200,"value_ids":[200],"scale_id":200,"property_name":"Primary color","values":["Black"]}],"offerings":[{"price":"4.00","quantity":15,"is_enabled":1}]}],"quantity_on_property":"200","sku_on_property":"200"}';
      
    try {
        $header = array("Authorization: Bearer ".$access_token,"x-api-key: ".$api_key,"Content-Type:application/json");
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://openapi.etsy.com/v3/application/listings/'.$listing_id.'/inventory',
            CURLOPT_HTTPHEADER => $header,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CUSTOMREQUEST => 'PUT',
            
            CURLOPT_POSTFIELDS =>$inv // Remove json_encode and update this line
        ));
        $response_body1 = curl_exec($curl);
        curl_close($curl);	
        $inventoryData = json_decode($response_body1);
        print_r($inventoryData);
        if(isset($inventoryData->error) && $inventoryData->error == 'invalid_token'){  	 
            $client = new Etsy\OAuth\Client($api_key);
            $refreshTokenArray = $client->refreshAccessToken($refresh_token);
            if(isset($refreshTokenArray->access_token)){
                $access_token = $refreshTokenArray->access_token;
                $refresh_token = $refreshTokenArray->refresh_token;
                $conn->query("update etsy_keys set access_token = '".mysqli_real_escape_string($conn, $access_token)."',refresh_token = '".mysqli_real_escape_string($conn, $refresh_token)."', updated_at = now() where user_id = ".$user_id); 
                $header = array("Authorization: Bearer ".$access_token,"x-api-key: ".$api_key,"Content-Type:application/json");
                $curl = curl_init();
                curl_setopt_array($curl, array(
                    CURLOPT_URL => 'https://openapi.etsy.com/v3/application/listings/'.$listing_id.'/inventory',
                    CURLOPT_HTTPHEADER => $header,
                    CURLOPT_SSL_VERIFYPEER => false,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_CUSTOMREQUEST => 'PUT',
                    CURLOPT_POSTFIELDS =>$inv // Remove json_encode and update this line
                ));
                $response_body1 = curl_exec($curl);
                curl_close($curl);	
                $inventoryData = json_decode($response_body1);
                 print_r($inventoryData);
            }
        }
        
        //print_r($inventoryData);
    } catch (OAuthException $e) {
        print_r($e);
        error_log($e->getMessage());
        error_log(print_r($oauth->getLastResponse(), true));
        error_log(print_r($oauth->getLastResponseInfo(), true));
    }
    "echo after sucess response";
    print_r($inventoryData);
?>