package errorcode

import (
	"context"

	"github.com/tidbcloud/pkgv2/bizerr"
	"github.com/tidbcloud/pkgv2/svcid"
)

var (
	InvalidCluster                 = NewCDCBusinessErrCode(1, "Invalid cluster")
	InvalidChangefeed              = NewCDCBusinessErrCode(2, "Invalid changefeed")
	AffectedNoRows                 = NewCDCBusinessErrCode(3, "Affected no rows")
	TestConnectionTimeout          = NewCDCBusinessErrCode(4, "Test connection timeout")
	CreateChangefeedUnretryableErr = NewCDCBusinessErrCode(5, "Create changefeed unretryable error")
	ChangefeedStateMisMatch        = NewCDCBusinessErrCode(6, "Changefeed state mismatch")
)

func NewCDCBusinessErrCode(offset int, memo string) bizerr.ErrCode {
	return bizerr.NewBusinessErrCode(svcid.ServerlessService, offset+CDCErrOffset, memo)
}

var CDCErrorMessages = map[bizerr.ErrCode]string{
	bizerr.ErrCodeParams:         "invalid params",
	InvalidCluster:               "invalid cluster",
	InvalidChangefeed:            "invalid changefeed",
	ErrCodeInvalidOrderby:        "invalid orderby",
	bizerr.ErrCodeRecordNotFound: "changefeed not found",
	AffectedNoRows:               "changefeed not found or not allowed to update",
	TestConnectionTimeout:        "connect to downstream timeout, please confirm the endpoint you provide is reachable and retry later. If retry can not solve the problem, please contact technical support.",
	ChangefeedStateMisMatch:      "The changefeed state is incorrect and not auto-recoverable. Please contact technical support.",
}

func PrepareCDCErrorMessage(ctx context.Context, err error) bizerr.BizError {
	return PrepareErrorMessage(ctx, err, CDCErrorMessages)
}
