package com.jeesharp.business.modules.common.bizscene.support;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jeesharp.business.modules.common.bizscene.vo.BizSceneOptionVO;
import lombok.Data;

/**
 * 从 {@code cfg_biz_scene} 解析选品配置：默认 {@code admin_list_url}，可被 {@code ext_json.pickers} 覆盖。
 *
 * @author jeesharp
 * @since 2026-07-03
 */
public final class BizScenePickerSupport {

    public static final String PICKER_COUPON_SCOPE = "COUPON_SCOPE";

    public static final String PICKER_BANNER_TARGET = "BANNER_TARGET";

    private BizScenePickerSupport() {
    }

    /**
     * 解析指定 picker 的管理端列表元数据。
     */
    public static PickerFields resolve(BizSceneOptionVO scene, String pickerCode) {
        PickerFields fields = new PickerFields();
        if (scene == null) {
            return fields;
        }
        fields.setListUrl(trimToNull(scene.getAdminListUrl()));
        fields.setKeywordField(trimToNull(scene.getKeywordField()));
        fields.setNameField(trimToNull(scene.getNameField()));

        if (StrUtil.isBlank(scene.getExtJson()) || StrUtil.isBlank(pickerCode)) {
            return fields;
        }
        JSONObject root = JSONUtil.parseObj(scene.getExtJson());
        JSONObject pickers = root.getJSONObject("pickers");
        if (pickers == null) {
            return fields;
        }
        JSONObject picker = pickers.getJSONObject(pickerCode.trim());
        if (picker == null) {
            return fields;
        }
        if (StrUtil.isNotBlank(picker.getStr("listUrl"))) {
            fields.setListUrl(picker.getStr("listUrl").trim());
        }
        if (StrUtil.isNotBlank(picker.getStr("keywordField"))) {
            fields.setKeywordField(picker.getStr("keywordField").trim());
        }
        if (StrUtil.isNotBlank(picker.getStr("nameField"))) {
            fields.setNameField(picker.getStr("nameField").trim());
        }
        if (StrUtil.isNotBlank(picker.getStr("itemLabel"))) {
            fields.setItemLabel(picker.getStr("itemLabel").trim());
        }
        return fields;
    }

    /**
     * 解析优惠券适用项展示名：picker.itemLabel → coupon.scopeItemLabel。
     */
    public static String resolveCouponScopeItemLabel(BizSceneOptionVO scene) {
        if (scene == null) {
            return null;
        }
        PickerFields picker = resolve(scene, PICKER_COUPON_SCOPE);
        if (StrUtil.isNotBlank(picker.getItemLabel())) {
            return picker.getItemLabel();
        }
        if (StrUtil.isBlank(scene.getExtJson())) {
            return null;
        }
        JSONObject root = JSONUtil.parseObj(scene.getExtJson());
        JSONObject coupon = root.getJSONObject("coupon");
        if (coupon == null) {
            return null;
        }
        String label = coupon.getStr("scopeItemLabel");
        return StrUtil.isBlank(label) ? null : label.trim();
    }

    private static String trimToNull(String value) {
        if (StrUtil.isBlank(value)) {
            return null;
        }
        return value.trim();
    }

    /**
     * 管理端选品列表元数据。
     */
    @Data
    public static class PickerFields {

        private String listUrl;

        private String keywordField;

        private String nameField;

        private String itemLabel;
    }
}
