package errorcode

import (
	"context"

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

var (
	ErrCodeEmptyBackupID                  = NewBackupBusinessErrCode(1, "ErrCodeEmptyBackupID")
	ErrCodeEmptyRestoreRequest            = NewBackupBusinessErrCode(2, "ErrCodeEmptyRestoreRequest")
	ErrCodeEmptyClusterIDInPITR           = NewBackupBusinessErrCode(3, "ErrCodeEmptyClusterIDInPITR")
	ErrCodeEmptyBackupTimeInPITR          = NewBackupBusinessErrCode(4, "ErrCodeEmptyBackupTimeInPITR")
	ErrCodeInvalidClusterID               = NewBackupBusinessErrCode(5, "ErrCodeInvalidClusterID")
	ErrCodeRestorePITRWithinOneMinute     = NewBackupBusinessErrCode(6, "ErrCodeRestorePITRWithinOneMinute")
	ErrCodeRestorePITROutOfRange          = NewBackupBusinessErrCode(7, "ErrCodeRestorePITROutOfRange")
	ErrCodeEmptyScheduleTime              = NewBackupBusinessErrCode(10, "ErrCodeEmptyScheduleTime")
	ErrCodeFreeClusterNotSupport          = NewBackupBusinessErrCode(11, "ErrCodeFreeClusterNotSupport")
	ErrCodeInvalidScheduleTime            = NewBackupBusinessErrCode(12, "ErrCodeInvalidScheduleTime")
	ErrCodeRestoreToNewWithoutClusterInfo = NewBackupBusinessErrCode(13, "ErrCodeRestoreToNewWithoutClusterInfo")
	ErrCodeUnsupportedRestoreType         = NewBackupBusinessErrCode(14, "ErrCodeUnsupportedRestoreType")
	ErrCodeRestorePITRNotAllowed          = NewBackupBusinessErrCode(15, "ErrCodeRestorePITRNotAllowed")
	ErrCodeClusterNotSupportRestoreToNew  = NewBackupBusinessErrCode(16, "ErrCodeClusterNotSupportRestoreToNew")
	ErrCodeRecycleBinRestoreOnlyToNew     = NewBackupBusinessErrCode(17, "ErrCodeRecycleBinRestoreOnlyToNew")
)

var BackupErrorMessages = map[bizerr.ErrCode]string{
	bizerr.ErrCodeRecordNotFound:          "Backup not found, please check your backup id.",
	ErrCodeNegativePageSize:               "Negative page size, please check your page size.",
	ErrCodeEmptyClusterID:                 "Empty cluster id, please check your cluster id.",
	ErrCodeInvalidPageToken:               "Invalid page token, please check your page token.",
	ErrCodeInvalidOrderby:                 "Invalid order by.",
	ErrCodeEmptyRestoreRequest:            "Empty body of restore request, you must specify one of backup_id or point_in_time.",
	ErrCodeEmptyBackupID:                  "Empty backup id, you must specify the backup id when you restore with the backup.",
	ErrCodeEmptyClusterIDInPITR:           "Empty cluster id, you must specify the cluster id when you restore with point-int-time.",
	ErrCodeEmptyBackupTimeInPITR:          "Empty backup time, you must specify the backup time when you restore with point-int-time.",
	ErrCodeInvalidClusterID:               "Invalid cluster id, cluster id must be a number",
	ErrCodeClusterNotFind:                 "Cluster not found, please check your cluster id.",
	ErrCodeClusterNotServerless:           "Cluster is not TiDB Starter or Essential cluster, please check your cluster id.",
	ErrCodeClusterNotRunning:              "Cluster is not available, only support restore to an available cluster.",
	ErrCodeRestorePITRWithinOneMinute:     "Point-in-time restore within one minute after cluster creation is not allowed.",
	ErrCodeRestorePITROutOfRange:          "Point-in-time restore out of range.",
	ErrCodeEmptyScheduleTime:              "Schedule time is required, please check your schedule time.",
	ErrCodeFreeClusterNotSupport:          "Free cluster does not support this operation.",
	ErrCodeInvalidScheduleTime:            "Invalid schedule time, schedule time must be HH:mm (every half an hour).",
	ErrCodeRestoreToNewWithoutClusterInfo: "Missing required cluster information when restore to new cluster.",
	ErrCodeUnsupportedRestoreType:         "Unsupported restore type, please specify one of the [INPLACE,NEW]",
	ErrCodeRestorePITRNotAllowed:          "Point-in-time restore is not allowed for the cluster without Spending Limit.",
	ErrCodeClusterNotSupportRestoreToNew:  "Clusters larger than 1 TiB do not support restoring to new cluster. Contact us for larger size.",
	ErrCodeRecycleBinRestoreOnlyToNew:     "Recycle bin backups can only be restored to a new cluster.",
}

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

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