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

$user_id = 2241;

$configResult = $conn->query("select * from etsy_keys where valid = 1 and user_id = 2241");
// echo $configResult;
echo "ddd";
if ($configResult->num_rows > 0) {
    $configRow = $configResult->fetch_assoc();
    $shop_id = $configRow['shop_id'];
    echo $shop_id;
    $access_token = $configRow['access_token'];
    $refresh_token = $configRow['refresh_token'];
    $api_key = $etsyConfig['api_key'];
    //  echo "changedeep14" ;
    //  echo $api_key ;
    //  echo $refresh_token ;
    //  echo  $access_token ;
    //  echo "changedeep17" ;
    $existingSKUs = array();
    $result = $conn->query("select distinct etsylistingid from products where user_id = " . $user_id);
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $existingSKUs[] = $row['etsylistingid'];
        }
    }


    // fetchCatalog($user_id, $shop_id, $access_token, $refresh_token, $existingSKUs, $api_key);
//}
}

$oauth_url = 'https://api.etsy.com/v3/public/oauth/token';


$post_data = array(
    'grant_type' => 'refresh_token',
    'refresh_token' =>  $refresh_token,
    'client_id' => $api_key,
    'client_secret' => $api_secret,
);

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $oauth_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));

// Execute cURL session and get the response
$response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
    echo 'cURL Error: ' . curl_error($ch);
    // Handle error if needed
}

// Close cURL session
curl_close($ch);

// Process the response
if ($response) {
    $data = json_decode($response, true);

    // Now you can access the new Access Token and other data
    $access_token = $data['access_token'];
     //echo "ffffff";
     //echo  $access_token;
   // echo "kkkk";
    // Save the new Access Token in your database or session for future API requests
    // ...
} else {
    echo 'Error refreshing Access Token';
    // Handle error if needed
}


// 1536424044

$listing_data = array(

    // "quantity"=> 1,
    // "title"=>"Test Swaches",
    // "description"=>"Test Swaches",
    // "price"=>"12.00","shipping_profile_id"=>86894355482,
    // "recipient"=>"",
    // "occasion"=>"",
    // "style"=>"",
    // "state"=>"active","shop_section_id"=>26059770,
    // "when_made"=>"2020_2023",
    // "is_supply"=>"true",
    // "who_made"=>"i_did",
    // "taxonomy_id"=>1,"processing_min"=>1,
    // "processing_max"=>1,
    // "should_auto_renew"=>"false",
    // "non_taxable"=> "false" 
       "state"=>"active"

);

$url = "https://openapi.etsy.com/v3/application/shops/13151699/listings/1536424044";
// https://openapi.etsy.com/v3/application/shops/{shop_id}/listings/{listing_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 => $url,
    CURLOPT_HTTPHEADER => $header,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => json_encode(array('image' => $file)),
    CURLOPT_CUSTOMREQUEST => 'POST'


));

$r_body = curl_exec($curl);
curl_close($curl);
$r = json_decode($r_body);
echo "ssss";
print_r($r);

// Handle the response
// if ($response === false) {
//     echo "Error occurred: " . curl_error($ch);
// } else {
//     $json_response = json_decode($response, true);
//     if (isset($json_response['results']) && count($json_response['results']) > 0) {
//         $listing_id = $json_response['results'][0]['listing_id'];
//         echo "Listing added successfully. Listing ID: " . $listing_id;
//     } else {
//         echo "Failed to add listing. Error: " . $json_response['error']['message'];
//     }
// }
