package errors

type ErrorCode int32

const SUCCESS = 0

const (
	PanicError          ErrorCode = -1
	UnrecognizedError   ErrorCode = -2
	K8SClientError      ErrorCode = -3
	AWSClientError      ErrorCode = -4
	APINotFound         ErrorCode = -5
	AliCloudClientError ErrorCode = -6
	ObjectNotFoundError ErrorCode = -7
	InternalServerError ErrorCode = -8
	AuthError           ErrorCode = -9

	// define common error

	IOReadFileError    ErrorCode = 1001
	B64DecodeError     ErrorCode = 1002
	ParameterInvalid   ErrorCode = 1003
	MarshalError       ErrorCode = 1004
	UnmarshalError     ErrorCode = 1005
	UnsupportedRegion  ErrorCode = 1006
	EmptyEnvError      ErrorCode = 1007
	ConvertError       ErrorCode = 1008
	ParamParseError    ErrorCode = 1009
	AbsenceOfConfig    ErrorCode = 1010
	UnsupportedBizType ErrorCode = 1011
	ConfigInvalid      ErrorCode = 1012

	// define global server error

	GetRequestParamError         ErrorCode = 10001
	NoSupportRequestMethodError  ErrorCode = 10002
	GetRegionalRouteAddressError ErrorCode = 10003
	HttpClientResponseError      ErrorCode = 10004
	GenStsCredentialError        ErrorCode = 10005
	TokenFileNotExistsError      ErrorCode = 10006
	CreateGlobalMetaError        ErrorCode = 10007
	UpdateGlobalMetaError        ErrorCode = 10008
	LoadGlobalMetaError          ErrorCode = 10009
	DetailGlobalMetaError        ErrorCode = 10010
	WatchGlobalMetaError         ErrorCode = 10011
	DuplicatedSubscription       ErrorCode = 10012
	SubscriptionStatusConflict   ErrorCode = 10013
	SubscriptionNotFound         ErrorCode = 10014

	// define regional server error

	DuplicatedId                    ErrorCode = 20001
	DuplicatedCluster               ErrorCode = 20002
	ClusterNotFound                 ErrorCode = 20003
	UnsupportedMonitorSpec          ErrorCode = 20004
	RoleNotFound                    ErrorCode = 20005
	ClusterStatusConflict           ErrorCode = 20006
	DuplicatedRoleName              ErrorCode = 20007
	DuplicatedBucketName            ErrorCode = 20008
	DuplicatedNamespace             ErrorCode = 20009
	BucketNotFound                  ErrorCode = 20010
	ConfigMapNotFound               ErrorCode = 20011
	MethodNotSupported              ErrorCode = 20012
	PrometheusRuleNotFound          ErrorCode = 20013
	PrometheusRuleIsEmpty           ErrorCode = 20014
	RoleBindingNotFound             ErrorCode = 20015
	PrometheusRuleTemplateIDIsEmpty ErrorCode = 20016
	PVCNotFound                     ErrorCode = 20017
	PrometheusRuleStatusNotValid    ErrorCode = 20018
	PrometheusRuleCanNotBeEdit      ErrorCode = 20019
	PrometheusRuleExprNotValid      ErrorCode = 20020
	ClusterAgentNotSet              ErrorCode = 20021
	IntegrationTaskNotFound         ErrorCode = 20022
	IntegrationTaskStatusConflict   ErrorCode = 20023
	DuplicatedIntegrationTask       ErrorCode = 20024

	// HelmRelease operation errors
	HelmReleaseNotFound    ErrorCode = 30001
	HelmReleaseUpdateError ErrorCode = 30002
	HelmReleaseConfigError ErrorCode = 30003
	BasicAuthUpdateError   ErrorCode = 30004

	DuplicatedEvaluationName ErrorCode = 90001
	EvaluationReadError      ErrorCode = 90002
	EvaluationWriteError     ErrorCode = 90003
	EvaluationNotFound       ErrorCode = 90004
	ModelReadError           ErrorCode = 90005
	ModelWriteError          ErrorCode = 90006
	ModelNotFound            ErrorCode = 90007
)

type ErrorCodeExplanation struct {
	explanation string
	httpCode    int
}

func (t ErrorCode) GetHttpCode() int {
	return explanationContainer[t].httpCode
}

func (t ErrorCode) Equal(code int32) bool {
	return code == int32(t)
}

func (t ErrorCode) Explain() string {
	return explanationContainer[t].explanation
}

var explanationContainer = map[ErrorCode]ErrorCodeExplanation{
	PanicError:          {"panic", 500},
	UnrecognizedError:   {"unrecognized error", 500},
	K8SClientError:      {"k8s client error", 500},
	AWSClientError:      {"aws client error", 500},
	InternalServerError: {"internal server error", 500},
	AuthError:           {"auth error", 401},
	APINotFound:         {"api not found", 404},

	ParameterInvalid:   {"parameter is invalid", 400},
	MarshalError:       {"marshal error", 500},
	UnmarshalError:     {"unmarshal error", 500},
	UnsupportedRegion:  {"vendor or region is not supported", 400},
	IOReadFileError:    {"io read file error", 500},
	B64DecodeError:     {"base64 decode error", 500},
	EmptyEnvError:      {"empty env error", 400},
	ConvertError:       {"convert error", 400},
	ParamParseError:    {"parameter parse error", 400},
	AbsenceOfConfig:    {"absence of config", 500},
	UnsupportedBizType: {"biz_type is not supported", 400},
	ConfigInvalid:      {"config is invalid", 500},

	DuplicatedId:                    {"duplicated cluster id", 409},
	DuplicatedCluster:               {"duplicated cluster", 409},
	ClusterNotFound:                 {"cluster is not found", 404},
	RoleNotFound:                    {"role is not found", 404},
	ClusterStatusConflict:           {"cluster status is conflicted", 409},
	DuplicatedRoleName:              {"duplicated role name", 400},
	DuplicatedBucketName:            {"duplicated bucket name", 400},
	DuplicatedNamespace:             {"duplicated namespace", 400},
	BucketNotFound:                  {"bucket is not found", 404},
	ConfigMapNotFound:               {"configmap is not found", 404},
	MethodNotSupported:              {"method is not supported", 405},
	PrometheusRuleNotFound:          {"prometheus rule not found", 404},
	RoleBindingNotFound:             {"rolebinding not found", 404},
	PrometheusRuleIsEmpty:           {"prometheus rule is empty", 400},
	PrometheusRuleTemplateIDIsEmpty: {"prometheus rule template id is empty", 400},
	PrometheusRuleStatusNotValid:    {"prometheus rule template id is empty", 400},
	PrometheusRuleCanNotBeEdit:      {"prometheus rule template id is empty", 403},
	DuplicatedSubscription:          {"duplicated subscription", 409},
	SubscriptionStatusConflict:      {"subscription status is conflicted", 409},
	SubscriptionNotFound:            {"subscription is not found", 404},
	PVCNotFound:                     {"pvc is not found", 404},
	IntegrationTaskNotFound:         {"integration task is not found", 404},
	IntegrationTaskStatusConflict:   {"integration task status is conflicted", 409},
	DuplicatedIntegrationTask:       {"duplicated integration task", 409},

	UnsupportedMonitorSpec: {"unsupported monitor spec", 400},

	HelmReleaseNotFound:    {"helm release not found", 404},
	HelmReleaseUpdateError: {"helm release update failed", 500},
	HelmReleaseConfigError: {"helm release configuration error", 400},
	BasicAuthUpdateError:   {"basic auth update failed", 500},

	GetRequestParamError:         {"get request param error", 400},
	NoSupportRequestMethodError:  {"no support for request method error", 400},
	GetRegionalRouteAddressError: {"get regional route address error", 400},
	HttpClientResponseError:      {"http client response error", 500},
	GenStsCredentialError:        {"gen sts credential error", 500},
	TokenFileNotExistsError:      {"token file not exists error", 500},
	CreateGlobalMetaError:        {"create global meta error", 500},
	UpdateGlobalMetaError:        {"update global meta error", 500},
	LoadGlobalMetaError:          {"load global meta error", 500},
	DetailGlobalMetaError:        {"detail global meta error", 500},
	WatchGlobalMetaError:         {"watch global meta error", 500},

	DuplicatedEvaluationName: {"duplicated evaluation name", 400},
	EvaluationReadError:      {"evaluation read error", 500},
	EvaluationWriteError:     {"evaluation write error", 500},
	EvaluationNotFound:       {"evaluation not found", 404},
	ModelReadError:           {"model read error", 500},
	ModelWriteError:          {"model write error", 500},
	ModelNotFound:            {"model not found", 404},
}
