package com.ruoyi.erp.request;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.List;

/**
 * 创建商品（单个）请求
 *
 * 对应接口：POST /api/open/product/create
 */
public class ProductCreateRequest extends ERPRequestBase {
	private final JSONArray channelPv = new JSONArray();
	private final JSONArray publishShop = new JSONArray();
	private final JSONArray skuItems = new JSONArray();

	public ProductCreateRequest(IERPAccount erpAccount) {
		super("https://open.goofish.pro/api/open/product/create", erpAccount);
	}

	public void setItemBizType(int itemBizType) {
		ensureRequestBodyInitialized();
		this.requestBody.put("item_biz_type", itemBizType);
	}

	public void setSpBizType(int spBizType) {
		ensureRequestBodyInitialized();
		this.requestBody.put("sp_biz_type", spBizType);
	}

	public void setChannelCatId(String channelCatId) {
		ensureRequestBodyInitialized();
		this.requestBody.put("channel_cat_id", channelCatId);
	}

	public void addChannelPv(String propertyId, String propertyName, String valueId, String valueName) {
		JSONObject pv = new JSONObject();
		pv.put("property_id", propertyId);
		pv.put("property_name", propertyName);
		pv.put("value_id", valueId);
		pv.put("value_name", valueName);
		channelPv.add(pv);
		ensureRequestBodyInitialized();
		this.requestBody.put("channel_pv", channelPv);
	}

	public void setPrice(long price) {
		ensureRequestBodyInitialized();
		this.requestBody.put("price", price);
	}

	public void setOriginalPrice(Long originalPrice) {
		if (originalPrice == null) return;
		ensureRequestBodyInitialized();
		this.requestBody.put("original_price", originalPrice);
	}

	public void setExpressFee(long expressFee) {
		ensureRequestBodyInitialized();
		this.requestBody.put("express_fee", expressFee);
	}

	public void setStock(int stock) {
		ensureRequestBodyInitialized();
		this.requestBody.put("stock", stock);
	}

	public void setOuterId(String outerId) {
		if (outerId == null) return;
		ensureRequestBodyInitialized();
		this.requestBody.put("outer_id", outerId);
	}

	public void setStuffStatus(Integer stuffStatus) {
		if (stuffStatus == null) return;
		ensureRequestBodyInitialized();
		this.requestBody.put("stuff_status", stuffStatus);
	}

	public void addPublishShop(String userName, int province, int city, int district, String title, String content, List<String> images, String whiteImages, String serviceSupport) {
		JSONObject shop = new JSONObject();
		shop.put("user_name", userName);
		shop.put("province", province);
		shop.put("city", city);
		shop.put("district", district);
		shop.put("title", title);
		shop.put("content", content);
		if (images != null && !images.isEmpty()) {
			JSONArray imgs = new JSONArray();
			imgs.addAll(images);
			shop.put("images", imgs);
		}
		if (whiteImages != null) {
			shop.put("white_images", whiteImages);
		}
		if (serviceSupport != null) {
			shop.put("service_support", serviceSupport);
		}
		publishShop.add(shop);
		ensureRequestBodyInitialized();
		this.requestBody.put("publish_shop", publishShop);
	}

	public void addSkuItem(long price, int stock, String skuText, String outerId) {
		JSONObject sku = new JSONObject();
		sku.put("price", price);
		sku.put("stock", stock);
		sku.put("sku_text", skuText);
		if (outerId != null) {
			sku.put("outer_id", outerId);
		}
		skuItems.add(sku);
		ensureRequestBodyInitialized();
		this.requestBody.put("sku_items", skuItems);
	}

	public void setBookData(JSONObject bookData) { putIfNotNull("book_data", bookData); }
	public void setFoodData(JSONObject foodData) { putIfNotNull("food_data", foodData); }
	public void setReportData(JSONObject reportData) { putIfNotNull("report_data", reportData); }
	public void setFlashSaleType(Integer flashSaleType) { if (flashSaleType != null) { ensureRequestBodyInitialized(); this.requestBody.put("flash_sale_type", flashSaleType); } }
	public void setAdventData(JSONObject adventData) { putIfNotNull("advent_data", adventData); }
	public void setInspectData(JSONObject inspectData) { putIfNotNull("inspect_data", inspectData); }
	public void setBrandData(JSONObject brandData) { putIfNotNull("brand_data", brandData); }
	public void setDetailImages(JSONArray images) { if (images != null) { ensureRequestBodyInitialized(); this.requestBody.put("detail_images", images); } }
	public void setSkuImages(JSONArray images) { if (images != null) { ensureRequestBodyInitialized(); this.requestBody.put("sku_images", images); } }

	private void putIfNotNull(String key, Object value) {
		if (value != null) {
			ensureRequestBodyInitialized();
			this.requestBody.put(key, value);
		}
	}

	private void ensureRequestBodyInitialized() {
		if (this.requestBody == null) {
			this.requestBody = new JSONObject();
		}
	}
}


