package com.yaoai.common.response;

import lombok.Data;
import org.slf4j.MDC;

/**
 * 统一响应包装
 * <pre>
 * 成功: {"success":true,"data":{...},"requestId":"..."}
 * 失败: {"success":false,"error":{"code":"...","message":"..."},"requestId":"..."}
 * </pre>
 */
@Data
public class ApiResponse<T> {

    private boolean success;
    private T data;
    private ErrorDetail error;
    private String requestId;
    private long timestamp;

    @Data
    public static class ErrorDetail {
        private String code;
        private String message;

        public ErrorDetail(String code, String message) {
            this.code = code;
            this.message = message;
        }
    }

    private ApiResponse() {
        this.requestId = MDC.get("requestId");
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> ApiResponse<T> success(T data) {
        ApiResponse<T> resp = new ApiResponse<>();
        resp.success = true;
        resp.data = data;
        return resp;
    }

    public static <T> ApiResponse<T> success() {
        return success(null);
    }

    public static <T> ApiResponse<T> error(String code, String message) {
        ApiResponse<T> resp = new ApiResponse<>();
        resp.success = false;
        resp.error = new ErrorDetail(code, message);
        return resp;
    }
}
