package com.yaoai.common.result;

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

@Data
public class Result<T> {

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

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

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

    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        result.success = true;
        result.data = data;
        return result;
    }

    public static <T> Result<T> ok() {
        return ok(null);
    }

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