package com.jeesharp.business.modules.pay.paysubmerchantapplyment.convert;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.entity.PaySubMerchantCancel;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.enums.PaySubMerchantCancelStateEnum;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.enums.PaySubMerchantCancelValidateResultEnum;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.enums.PaySubMerchantCancelWithdrawEnum;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantCancelAccountInfoVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantCancelBlockReasonVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantCancelListVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantCancelStatusVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantCancelSubmitVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantCancelValidateVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;

/**
 * 商户注销 MapStruct 转换器。
 *
 * @author jeesharp
 * @since 2026-06-15
 */
@Mapper
public interface PaySubMerchantCancelConvert {

    PaySubMerchantCancelConvert INSTANCE = Mappers.getMapper(PaySubMerchantCancelConvert.class);

    @Mapping(target = "withdrawName", source = "withdraw", qualifiedByName = "withdrawToName")
    @Mapping(target = "cancelStateName", source = "cancelState", qualifiedByName = "cancelStateToName")
    PaySubMerchantCancelListVO toListVO(PaySubMerchantCancel entity);

    List<PaySubMerchantCancelListVO> toListVOList(List<PaySubMerchantCancel> list);

    @Mapping(target = "withdrawName", source = "withdraw", qualifiedByName = "withdrawToName")
    @Mapping(target = "cancelStateName", source = "cancelState", qualifiedByName = "cancelStateToName")
    @Mapping(target = "wxResponseJson", ignore = true)
    PaySubMerchantCancelStatusVO toStatusVO(PaySubMerchantCancel entity);

    @Mapping(target = "cancelStateName", source = "cancelState", qualifiedByName = "cancelStateToName")
    PaySubMerchantCancelSubmitVO toSubmitVO(PaySubMerchantCancel entity);

    default PaySubMerchantCancelValidateVO toValidateVO(JSONObject wx, String wxJson) {
        PaySubMerchantCancelValidateVO vo = new PaySubMerchantCancelValidateVO();
        if (wx == null) {
            vo.setWxResponseJson(wxJson);
            return vo;
        }
        vo.setSubMchId(wx.getStr("sub_mchid"));
        vo.setMerchantState(wx.getStr("merchant_state"));
        vo.setMerchantStateName(merchantStateToName(vo.getMerchantState()));
        vo.setValidateResult(wx.getStr("validate_result"));
        vo.setValidateResultName(validateResultToName(vo.getValidateResult()));
        vo.setBlockReasons(parseBlockReasons(wx.getJSONArray("block_reasons")));
        vo.setAccountInfo(parseAccountInfo(wx.getJSONArray("account_info")));
        vo.setWxResponseJson(wxJson);
        return vo;
    }

    default List<PaySubMerchantCancelBlockReasonVO> parseBlockReasons(JSONArray arr) {
        if (arr == null || arr.isEmpty()) {
            return List.of();
        }
        List<PaySubMerchantCancelBlockReasonVO> list = new ArrayList<>();
        for (int i = 0; i < arr.size(); i++) {
            JSONObject item = arr.getJSONObject(i);
            if (item == null) {
                continue;
            }
            PaySubMerchantCancelBlockReasonVO row = new PaySubMerchantCancelBlockReasonVO();
            row.setType(item.getStr("type"));
            row.setDescription(item.getStr("description"));
            list.add(row);
        }
        return list;
    }

    default List<PaySubMerchantCancelAccountInfoVO> parseAccountInfo(JSONArray arr) {
        if (arr == null || arr.isEmpty()) {
            return List.of();
        }
        List<PaySubMerchantCancelAccountInfoVO> list = new ArrayList<>();
        for (int i = 0; i < arr.size(); i++) {
            JSONObject item = arr.getJSONObject(i);
            if (item == null) {
                continue;
            }
            PaySubMerchantCancelAccountInfoVO row = new PaySubMerchantCancelAccountInfoVO();
            row.setOutAccountType(item.getStr("out_account_type"));
            row.setAmount(item.getLong("amount"));
            list.add(row);
        }
        return list;
    }

    @Named("withdrawToName")
    default String withdrawToName(String withdraw) {
        return PaySubMerchantCancelWithdrawEnum.getDescByValue(withdraw);
    }

    @Named("cancelStateToName")
    default String cancelStateToName(String cancelState) {
        return PaySubMerchantCancelStateEnum.getDescByValue(cancelState);
    }

    @Named("merchantStateToName")
    default String merchantStateToName(String merchantState) {
        if (merchantState == null) {
            return "";
        }
        return switch (merchantState) {
            case "NORMAL" -> "正常";
            case "HAS_BEEN_CANCELLED" -> "已注销";
            default -> merchantState;
        };
    }

    @Named("validateResultToName")
    default String validateResultToName(String validateResult) {
        return PaySubMerchantCancelValidateResultEnum.getDescByValue(validateResult);
    }
}
