package com.malk.server.integration;

import com.malk.server.common.McException;
import com.malk.server.common.VenR;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

/**
 * 集成平台统一响应包装
 * <p>
 * 服务端返回结构：
 * <pre>
 * {
 *   "result": true|false,
 *   "error": "ERROR_CODE",
 *   "error_description": "可读错误描述",
 *   "data": {...} | [...]
 * }
 * </pre>
 */
@Data
@NoArgsConstructor
public class INTPR<T> extends VenR {

    /**
     * 业务成功标记
     */
    private boolean result;

    /**
     * 错误代码（result=false 时有值）
     */
    private String error;

    /**
     * 错误描述（result=false 时有值）
     */
    private String error_description;

    /**
     * 业务数据
     */
    private T data;

    /**
     * OAuth2 token endpoint 直接返回字段（非包装结构）
     */
    private String access_token;
    private String token_type;
    private Integer expires_in;
    private String scope;

    /**
     * 断言错误信息
     */
    @Override
    public void assertSuccess() {
        // ppExt: token endpoint 不走包装结构，access_token 非空即视为成功
        if (access_token != null) {
            return;
        }
        McException.assertException(!result, error, error_description, "integration");
    }

    /// ---------- 静态请求入口 ----------

    public static INTPR doPost(String url, Map header, Map param, Map body) {
        return (INTPR) VenR.doPost(url, header, param, body, VenR.RC_INTP);
    }

    public static INTPR doGet(String url, Map header, Map param) {
        return (INTPR) VenR.doGet(url, header, param, VenR.RC_INTP);
    }
}
