package com.cool.store.response.bigdata;

import com.cool.store.constants.CommonConstants;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.ResponseCodeEnum;
import com.cool.store.response.ResponseResult;
import lombok.Data;
import org.slf4j.MDC;

import java.text.MessageFormat;

/**
 * @Author suzhuhong
 * @Date 2025/4/1 10:49
 * @Version 1.0
 */
@Data
public class ApiResponse<T> {
    private String msg;
    private int code;
    private T data;
    private String stackTrace;
    private String requestId;


    public ApiResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.requestId = MDC.get(CommonConstants.REQUEST_ID);
    }

    public static<T> ApiResponse<T> success(T data) {
        return new ApiResponse(ResponseCodeEnum.SUCCESS.getCode(), "ok", data);
    }

    /**
     * 钱包系统 响应用200
     * @param data
     * @param <T>
     * @return
     */
    public static<T> ApiResponse<T> successByWallet(T data) {
        return new ApiResponse(ResponseCodeEnum.SUCCESS_WALLET.getCode(), "ok", data);
    }


    public static ApiResponse fail(ErrorCodeEnum responseEnum, Object... objects){
        String message = MessageFormat.format(responseEnum.getMessage(), objects);
        return new ApiResponse(responseEnum.getCode(), message, false);
    }

    public static<T> ApiResponse<T> error(ErrorCodeEnum errorCodeEnum) {
        return new ApiResponse(errorCodeEnum.getCode(), errorCodeEnum.getMessage(), null);
    }
}
