package errs

import (
	"fmt"

	"github.com/tidbcloud/dataservices/commons/bizerr"
	errors "github.com/tidbcloud/tiinf/errs"
)

var (
	NewError                   = errors.NewError
	NewBadRequest              = errors.NewBadRequest
	NewInternalDB              = errors.NewInternalDB
	NewInternalGeneralBusiness = errors.NewInternalGeneralBusiness
)

var (
	ErrAPIKeyNotFound          = errors.NewNotFound("api key not found", nil)
	ErrUnauthorized            = errors.NewUnauthorized("Authentication failure.", nil)
	ErrExceedSize              = errors.NewBadRequest("exceed size", nil)
	ErrInvalidDeleteEndpoint   = errors.NewBadRequest("The endpoint has been deployed, please undeploy and try again.", nil)
	ErrClusterUserJwtEmpty     = errors.NewBadRequest("cluster user/jwt empty", nil)
	ErrClusterNotSupportDbUser = errors.NewBadRequest("cluster not support dbuser", nil)
	ErrUnsupportedCrtType      = errors.NewBadRequest("unsupported credential type", nil)
	ErrNotSupportedAppType     = errors.NewBadRequest("Not supported app type.", nil)
	ErrInvalidSession          = errors.NewError(bizerr.ErrInvalidSession, "Sorry, your session has expired, please refresh the page and try again ^_^!", nil)
	ErrInvalidUserSQLStatement = errors.NewBadRequest("Invalid user sql statement", nil)
	ErrEndpointExecFailed      = errors.NewBadRequest("Endpoint exec failed", nil)
	ErrEndpointNotFound        = errors.NewNotFound("endpoint not found", nil)
	ErrOperationNotAllowed     = errors.NewBadRequest("Operation not allowed", nil)
	ErrDataappNotFound         = errors.NewNotFound("dataapp not found", nil)
	ErrUserClusterNotFound     = errors.NewNotFound("cluster not found", nil)
	ErrInvalidEvent            = errors.NewBadRequest("Invalid event data", nil)
	ErrDeploymentConflict      = errors.NewBadRequest("the dataapp has deployment conflict, only allow discarding draft.", nil)
	ErrEndpointVersionConflict = errors.NewError(bizerr.ErrVersionConflict, "Failed to update endpoint due to version conflict. Please refresh endpoint information before updating", nil)
	ErrPaginationConflict      = errors.NewError(bizerr.ErrPaginationConflict, "The endpoint parameters are in conflict with the predefined paging variables 'page' and 'page_size'. Kindly select an alternative option.", nil)
	ErrProhibitRetry           = errors.NewInternalGeneralBusiness("Prohibit retry", nil)
	ErrAPIKeyExpired           = errors.NewBadRequest("api key expired", nil)
)

func WrapGORMError(err error, format string, a ...any) error {
	return errors.WrapGORMError(err, fmt.Sprintf(format, a...))
}

func WrapRPCError(err error, format string, a ...any) error {
	return errors.NewInternalGRPC(fmt.Sprintf(format, a...)+" failed", err)
}

func WrapHTTPError(err error, format string, a ...any) error {
	return errors.NewInternalHTTP(fmt.Sprintf(format, a...)+" failed", err)
}

func IsClientErr(err error) bool {
	if err == nil {
		return false
	}
	if tierr, ok := err.(errors.Error); ok {
		return tierr.HTTPCode() >= 400 && tierr.HTTPCode() < 500
	}
	return false
}
