package com.ycwl.basic.integration.common.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponse<T> {
    private Integer code;
    private String message;
    private Boolean success;
    private T data;

    public static <T> CommonResponse<T> success() {
        return new CommonResponse<>(200, "OK", true, null);
    }

    public static <T> CommonResponse<T> success(T data) {
        return new CommonResponse<>(200, "OK", true, data);
    }

    public static <T> CommonResponse<T> success(String message, T data) {
        return new CommonResponse<>(200, message, true, data);
    }

    public static <T> CommonResponse<T> error(Integer code, String message) {
        return new CommonResponse<>(code, message, false, null);
    }

    public static <T> CommonResponse<T> error(String message) {
        return new CommonResponse<>(5000, message, false, null);
    }

    public boolean isSuccess() {
        if (success == null) { // compatible
            return code != null && code == 200;
        }
        return success;
    }
}