package com.liquidnet.service.kylin.utils;

import com.liquidnet.service.kylin.dto.vo.OrderRefundReasonVo;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 退款原因展示文案与校验工具
 */
public final class OrderRefundReasonUtils {

    private OrderRefundReasonUtils() {
    }

    /**
     * 校验已应用退款原因，并组装落库字段
     *
     * @param reasonId     原因配置 ID
     * @param reasonDetail 二级补充说明
     * @param appliedList  已应用原因列表（可从缓存取）
     */
    public static ResolvedRefundReason resolve(String reasonId, String reasonDetail, List<OrderRefundReasonVo> appliedList) {
        if (StringUtils.isBlank(reasonId)) {
            return ResolvedRefundReason.fail("请选择退款原因");
        }
        OrderRefundReasonVo matched = findByReasonId(reasonId, appliedList);
        if (matched == null) {
            return ResolvedRefundReason.fail("退款原因无效或未应用");
        }
        return resolve(matched, reasonDetail);
    }

    /**
     * 已拿到配置项时的校验与组装
     */
    public static ResolvedRefundReason resolve(OrderRefundReasonVo matched, String reasonDetail) {
        if (matched == null) {
            return ResolvedRefundReason.fail("退款原因无效或未应用");
        }
        String detail = StringUtils.trimToEmpty(reasonDetail);
        boolean needDetail = matched.getNeedDetail() != null && matched.getNeedDetail() == 1;
        if (needDetail && StringUtils.isBlank(detail)) {
            return ResolvedRefundReason.fail("请填写补充说明");
        }
        if (!needDetail) {
            detail = null;
        } else {
            detail = truncate(detail, 200);
        }
        String snapshot = buildReasonSnapshot(matched.getName(), detail);
        return ResolvedRefundReason.ok(matched.getReasonId(), detail, snapshot);
    }

    public static OrderRefundReasonVo findByReasonId(String reasonId, List<OrderRefundReasonVo> appliedList) {
        if (StringUtils.isBlank(reasonId) || appliedList == null) {
            return null;
        }
        for (OrderRefundReasonVo item : appliedList) {
            if (reasonId.equals(item.getReasonId())) {
                return item;
            }
        }
        return null;
    }

    /**
     * 组装 reason 快照：无二级写 name；有二级写 name：detail，截断至 200
     */
    public static String buildReasonSnapshot(String name, String reasonDetail) {
        String base = StringUtils.defaultString(name, "");
        if (StringUtils.isBlank(reasonDetail)) {
            return truncate(base, 200);
        }
        return truncate(base + "：" + reasonDetail.trim(), 200);
    }

    public static String truncate(String value, int maxLen) {
        if (value == null) {
            return "";
        }
        if (value.length() <= maxLen) {
            return value;
        }
        return value.substring(0, maxLen);
    }

    @Getter
    public static final class ResolvedRefundReason {
        private final boolean success;
        private final String errorMsg;
        private final String reasonId;
        private final String reasonDetail;
        private final String reasonSnapshot;

        private ResolvedRefundReason(boolean success, String errorMsg, String reasonId, String reasonDetail, String reasonSnapshot) {
            this.success = success;
            this.errorMsg = errorMsg;
            this.reasonId = reasonId;
            this.reasonDetail = reasonDetail;
            this.reasonSnapshot = reasonSnapshot;
        }

        public static ResolvedRefundReason ok(String reasonId, String reasonDetail, String reasonSnapshot) {
            return new ResolvedRefundReason(true, null, reasonId, reasonDetail, reasonSnapshot);
        }

        public static ResolvedRefundReason fail(String errorMsg) {
            return new ResolvedRefundReason(false, errorMsg, null, null, null);
        }
    }
}
