package com.hys.app.service.otherService.amazon;

import com.amazon.spapi.api.ListingsApi;
import com.amazon.spapi.client.ApiException;
import com.amazon.spapi.SellingPartnerAPIAA.LWAAuthorizationCredentials;
import com.amazon.spapi.SellingPartnerAPIAA.AWSAuthenticationCredentials;
import com.amazon.spapi.SellingPartnerAPIAA.AWSAuthenticationCredentialsProvider;
import com.amazon.spapi.model.catalogitems.Item;
import com.amazon.spapi.model.catalogitems.ItemProductTypes;
import com.amazon.spapi.model.listingsitems.ListingsItemPatchRequest;
import com.amazon.spapi.model.listingsitems.PatchOperation;

import java.util.*;

public class AmazonProductPusher {

    public static void pushProduct() {
        try {
            // 设置 LWA 凭证
            LWAAuthorizationCredentials lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder()
                    .clientId("YOUR_LWA_CLIENT_ID")
                    .clientSecret("YOUR_LWA_CLIENT_SECRET")
                    .refreshToken("YOUR_REFRESH_TOKEN")
                    .endpoint("https://api.amazon.com/auth/o2/token")
                    .build();

            // 设置 AWS 凭证
            AWSAuthenticationCredentials awsAuthenticationCredentials = AWSAuthenticationCredentials.builder()
                    .accessKeyId("YOUR_AWS_ACCESS_KEY")
                    .secretKey("YOUR_AWS_SECRET_KEY")
                    .region("YOUR_AWS_REGION")
                    .build();

            AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider = AWSAuthenticationCredentialsProvider.builder()
                    .roleArn("YOUR_ROLE_ARN")
                    .roleSessionName("session-name")
                    .build();

            // 初始化 ListingsApi
            ListingsApi listingsApi = new ListingsApi.Builder()
                    .lwaAuthorizationCredentials(lwaAuthorizationCredentials)
                    .awsAuthenticationCredentials(awsAuthenticationCredentials)
                    .awsAuthenticationCredentialsProvider(awsAuthenticationCredentialsProvider)
                    .endpoint("https://sellingpartnerapi-na.amazon.com")  // 根据你的区域选择正确的端点
                    .build();

            // 创建产品信息
            Map<String, Object> attributes = new HashMap<>();
            attributes.put("title", "Your Product Title");
            attributes.put("brand", "Your Brand");
            attributes.put("product_type", "PRODUCT_TYPE");
            // 添加其他必要的属性...

            List<PatchOperation> patchOperations = new ArrayList<>();
            patchOperations.add(new PatchOperation()
                    .op(PatchOperation.OpEnum.REPLACE)
                    .path("/attributes")
                    .value(new ArrayList<>()));

            ListingsItemPatchRequest patchRequest = new ListingsItemPatchRequest()
                    .productType("PRODUCT_TYPE")
                    .patches(patchOperations);

            // 推送产品
            listingsApi.patchListingsItem("YOUR_SELLER_ID",
                    "YOUR_SKU",
                    Arrays.asList("YOUR_MARKETPLACE_ID"),
                    patchRequest,
                    "PATCH_LISTINGS_ITEM");

            System.out.println("Product pushed successfully!");

        } catch (ApiException e) {
            System.err.println("Exception when calling ListingsApi#patchListingsItem");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        pushProduct();
    }
}