package errorcode

import "fmt"

var (
	Success        = &BusinessErr{code: 200, message: "成功"}
	ParamError     = &BusinessErr{code: 401, message: "参数错误"}
	ForbiddenError = &BusinessErr{code: 403, message: "权限不足"}
	NotFoundError  = &BusinessErr{code: 404, message: "请求地址未找到"}
	SystemError    = &BusinessErr{code: 405, message: "系统错误"}

	ClientNotFound  = &BusinessErr{code: 406, message: "未找到client_id"}
	SessionNotFound = &BusinessErr{code: 407, message: "未找到会话信息"}
	AuthNotFound    = &BusinessErr{code: 408, message: "身份验证失败"}
	KeyNotFound     = &BusinessErr{code: 409, message: "身份验证失败"}
	SysNotFound     = &BusinessErr{code: 410, message: "未找到系统信息"}
	SysCodeNotFound = &BusinessErr{code: 411, message: "未找到系统编码"}
	InvalidParam    = &BusinessErr{code: InvalidParamCode, message: "无效参数"}
	WorkflowError   = &BusinessErr{code: 501, message: "工作流过程错误"}
)

const (
	InvalidParamCode = 408
)

type BusinessErr struct {
	code    int
	message string
}

func (e *BusinessErr) Error() string {
	return e.message
}
func (e *BusinessErr) Code() int {
	return e.code
}

func (e *BusinessErr) Is(target error) bool {
	_, ok := target.(*BusinessErr)
	return ok
}

// CustomErr 自定义错误
func NewBusinessErr(code int, message string) *BusinessErr {
	return &BusinessErr{code: code, message: message}
}

func SysErrf(message string, arg ...any) *BusinessErr {
	return &BusinessErr{code: SystemError.code, message: fmt.Sprintf(message, arg)}
}

func SysErr(message string) *BusinessErr {
	return &BusinessErr{code: SystemError.code, message: message}
}

func ParamErrf(message string, arg ...any) *BusinessErr {
	return &BusinessErr{code: ParamError.code, message: fmt.Sprintf(message, arg)}
}

func ParamErr(message string) *BusinessErr {
	return &BusinessErr{code: ParamError.code, message: message}
}

func (e *BusinessErr) Wrap(err error) *BusinessErr {
	return NewBusinessErr(e.code, err.Error())
}

func WorkflowErr(message string) *BusinessErr {
	return NewBusinessErr(WorkflowError.code, message)
}

func ForbiddenErr(message string) *BusinessErr {
	return NewBusinessErr(ForbiddenError.code, message)
}
