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

import com.jeesharp.business.modules.pay.paysubmerchantapplyment.dto.PaySubMerchantApplymentSaveDTO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.entity.PaySubMerchantApplyment;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.enums.PaySubMerchantApplyLocalStatusEnum;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.enums.PaySubMerchantApplyStatusEnum;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.enums.PaySubMerchantApplymentApiTypeEnum;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantApplymentInfoVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantApplymentListVO;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantApplymentQueryVO;
import com.jeesharp.framework.common.pojo.convert.BasePojoConvert;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

/**
 * 特约商户进件 MapStruct 转换器。
 *
 * @author jeesharp
 * @since 2026-05-26
 */
@Mapper
public interface PaySubMerchantApplymentConvert extends BasePojoConvert<PaySubMerchantApplyment, PaySubMerchantApplymentSaveDTO,
        PaySubMerchantApplymentInfoVO, PaySubMerchantApplymentListVO, PaySubMerchantApplymentQueryVO> {

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

    @Override
    @Mapping(target = "applyStatusName", source = "applyStatus", qualifiedByName = "statusToName")
    @Mapping(target = "applymentApiTypeName", source = "applymentApiType", qualifiedByName = "apiTypeToName")
    PaySubMerchantApplymentInfoVO toInfoVO(PaySubMerchantApplyment entity);

    @Override
    @Mapping(target = "applyStatusName", source = "applyStatus", qualifiedByName = "statusToName")
    @Mapping(target = "applymentApiTypeName", source = "applymentApiType", qualifiedByName = "apiTypeToName")
    PaySubMerchantApplymentListVO toListSingleVO(PaySubMerchantApplyment entity);

    @Named("apiTypeToName")
    default String apiTypeToName(String apiType) {
        return PaySubMerchantApplymentApiTypeEnum.getDescByValue(apiType);
    }

    @Named("statusToName")
    default String statusToName(String status) {
        String normalized = normalizeStatusCode(status);
        String local = PaySubMerchantApplyLocalStatusEnum.getDescByValue(normalized);
        if (!local.equals(normalized)) {
            return local;
        }
        String merchantCancelled = PaySubMerchantApplyStatusEnum.getDescByValue(normalized);
        if (!merchantCancelled.isEmpty()) {
            return merchantCancelled;
        }
        return switch (normalized) {
            case "CHECKING" -> "资料校验中";
            case "ACCOUNT_NEED_VERIFY" -> "待账户验证";
            case "AUDITING" -> "审核中";
            case "REJECTED" -> "已驳回";
            case "NEED_SIGN", "SIGNING" -> "待签约";
            case "FINISH" -> "完成";
            case "FROZEN" -> "已冻结";
            case "CANCELED" -> "已作废";
            default -> status == null ? "" : status;
        };
    }

    default String normalizeStatusCode(String status) {
        if (status == null || status.isBlank()) {
            return "";
        }
        String upper = status.trim().toUpperCase();
        return switch (upper) {
            case "APPLYMENT_STATE_EDITTING" -> PaySubMerchantApplyLocalStatusEnum.DRAFT.getValue();
            case "APPLYMENT_STATE_AUDITING" -> "AUDITING";
            case "APPLYMENT_STATE_REJECTED" -> "REJECTED";
            case "APPLYMENT_STATE_TO_BE_CONFIRMED" -> "ACCOUNT_NEED_VERIFY";
            case "APPLYMENT_STATE_TO_BE_SIGNED", "APPLYMENT_STATE_SIGNING" -> "NEED_SIGN";
            case "APPLYMENT_STATE_FINISHED" -> "FINISH";
            case "APPLYMENT_STATE_CANCELED", "APPLYMENT_STATE_CANCELLED" -> "CANCELED";
            case "APPLYMENT_STATE_FROZEN" -> "FROZEN";
            default -> upper.startsWith("APPLYMENT_STATE_")
                    ? upper.substring("APPLYMENT_STATE_".length())
                    : status.trim();
        };
    }
}
