package com.ttstd.signaling.security;

/**
 * 鉴权/账号相关业务异常。
 *
 * <p>{@code publicMessage} 是可返回给客户端的模糊提示，
 * {@code detail} 仅用于服务端日志，避免向外泄露账号是否存在、设备是否在线等信息。
 */
public class AuthException extends RuntimeException {

    private final int status;
    private final String code;
    private final String publicMessage;

    public AuthException(int status, String code, String publicMessage) {
        this(status, code, publicMessage, publicMessage);
    }

    public AuthException(int status, String code, String publicMessage, String detail) {
        super(detail);
        this.status = status;
        this.code = code;
        this.publicMessage = publicMessage;
    }

    public int getStatus() { return status; }
    public String getCode() { return code; }
    public String getPublicMessage() { return publicMessage; }

    public static AuthException unauthorized(String detail) {
        return new AuthException(401, "UNAUTHORIZED", "认证失败", detail);
    }

    public static AuthException forbidden(String publicMessage, String detail) {
        return new AuthException(403, "FORBIDDEN", publicMessage, detail);
    }

    public static AuthException forbidden(String publicMessage) {
        return new AuthException(403, "FORBIDDEN", publicMessage, publicMessage);
    }

    public static AuthException badRequest(String publicMessage) {
        return new AuthException(400, "BAD_REQUEST", publicMessage);
    }

    public static AuthException notFound(String publicMessage) {
        return new AuthException(404, "NOT_FOUND", publicMessage);
    }

    public static AuthException tooManyRequests(String publicMessage) {
        return new AuthException(429, "TOO_MANY_REQUESTS", publicMessage);
    }
}
