package errorcode

import (
	"context"

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

var (
	ErrCodeInvalidFileType                 = NewExportBusinessErrCode(1, "ErrCodeInvalidFileType")
	ErrCodeInvalidTargetType               = NewExportBusinessErrCode(2, "ErrCodeInvalidTargetType")
	ErrCodeEmptyS3Uri                      = NewExportBusinessErrCode(3, "ErrCodeEmptyS3Uri")
	ErrCodeEmptyS3AccessKey                = NewExportBusinessErrCode(4, "ErrCodeEmptyS3AccessKey")
	ErrCodeCancelNotRunningExport          = NewExportBusinessErrCode(5, "ErrCodeCancelNotRunningExport")
	ErrCodeAlreadyCancelExport             = NewExportBusinessErrCode(6, "ErrCodeAlreadyCancelExport")
	ErrCodeUnsupportedAuthMethod           = NewExportBusinessErrCode(7, "ErrCodeUnsupportedAuthMethod")
	ErrCodeDeleteRunningExport             = NewExportBusinessErrCode(8, "ErrCodeDeleteRunningExport")
	ErrCodeLackDatabaseInLocal             = NewExportBusinessErrCode(9, "ErrCodeLackDatabaseInLocal")
	ErrCodeInvalidS3Uri                    = NewExportBusinessErrCode(10, "ErrCodeInvalidS3Uri")
	ErrCodeInvalidS3AccessKeyID            = NewExportBusinessErrCode(11, "ErrCodeInvalidS3AccessKeyID")
	ErrCodeExportNotSuccessful             = NewExportBusinessErrCode(14, "ErrCodeExportNotSuccessful")
	ErrCodeExportNotLocal                  = NewExportBusinessErrCode(15, "ErrCodeExportLocal")
	ErrCodeExportNoData                    = NewExportBusinessErrCode(16, "ErrCodeExportNoData")
	ErrCodeExportExpired                   = NewExportBusinessErrCode(17, "ErrCodeExportExpired")
	ErrCodeClusterNotFind                  = NewExportBusinessErrCode(18, "ErrCodeClusterNotFind")
	ErrCodeExportUpdateStateAffectedNoRows = NewExportBusinessErrCode(19, "ErrCodeExportUpdateStateAffectedNoRows")
	ErrCodeInvalidCompression              = NewExportBusinessErrCode(20, "ErrCodeInvalidCompression")
	ErrCodeInvalidSnapshotTime             = NewExportBusinessErrCode(24, "ErrCodeInvalidSnapshotTime")
	ErrCodeNotSupportSQLWithSQLQuery       = NewExportBusinessErrCode(25, "ErrCodeNotSupportSQLWithSQLQuery")
	ErrCodeInvalidTableFilter              = NewExportBusinessErrCode(26, "ErrCodeInvalidTableFilter")
	ErrCodeBothSQLAndTableExist            = NewExportBusinessErrCode(27, "ErrCodeBothSQLAndTableExist")
	ErrCodeInvalidSQL                      = NewExportBusinessErrCode(28, "ErrCodeInvalidSQL")
	ErrCodeInvalidWhere                    = NewExportBusinessErrCode(29, "ErrCodeInvalidWhere")
	ErrCodeEmptyS3RoleArn                  = NewExportBusinessErrCode(30, "ErrCodeEmptyS3RoleArn")
	ErrCodeEmptyS3Auth                     = NewExportBusinessErrCode(31, "ErrCodeEmptyS3Auth")
	ErrCodeGCSAuth                         = NewExportBusinessErrCode(32, "ErrCodeGCSAuth")
	ErrCodeAzBlobAuth                      = NewExportBusinessErrCode(33, "ErrCodeAzBlobAuth")
	ErrCodeInvalidParquetCompression       = NewExportBusinessErrCode(34, "ErrCodeInvalidParquetCompression")
	ErrCodeSpecifyCompressionInParquet     = NewExportBusinessErrCode(35, "ErrCodeSpecifyCompressionInParquet")
	ErrCodeExceedMaxFileNumber             = NewExportBusinessErrCode(36, "ErrCodeExceedMaxFileNumber")
	ErrCodeExportFromThrottled             = NewExportBusinessErrCode(37, "ErrCodeExportFromThrottled")
	ErrCodeExportStorageLimitExceed        = NewExportBusinessErrCode(38, "ErrCodeExportStorageLimitExceed")
	ErrCodeEmptyExport                     = NewExportBusinessErrCode(39, "ErrCodeEmptyExport")
	ErrCodeExportConcurrencyLimitExceeded  = NewExportBusinessErrCode(40, "ErrCodeExportConcurrencyLimitExceeded")
)

var ExportErrorMessages = map[bizerr.ErrCode]string{
	ErrCodeInvalidFileType:                 "Invalid file type, support SQL, CSV and PARQUET.",
	ErrCodeInvalidTargetType:               "Invalid target type, only support LOCAL, S3, GCS and AZURE_BLOB.",
	ErrCodeEmptyClusterID:                  "Empty cluster id, please check your cluster id.",
	ErrCodeEmptyS3Uri:                      "Empty S3 URI.",
	ErrCodeEmptyS3AccessKey:                "Empty S3 access key id or secret.",
	ErrCodeClusterNotRunning:               "Cluster not running, please check your cluster status.",
	ErrCodeUnsupportedClusterType:          "Unsupported cluster type, only support TiDB Starter/Essential cluster.",
	ErrCodeCancelNotRunningExport:          "Cancel export which is not running. Please check your export status.",
	ErrCodeNegativePageSize:                "Negative page size, please check your page size.",
	ErrCodeInvalidPageToken:                "Invalid page token, please check your page token.",
	bizerr.ErrCodeRecordNotFound:           "Export not found, please check your export id.",
	ErrCodeAlreadyCancelExport:             "Export already be canceled.",
	ErrCodeUnsupportedAuthMethod:           "Unsupported auth method, only support openapi and console now.",
	ErrCodeDeleteRunningExport:             "Can not delete running export, please cancel it first.",
	ErrCodeLackDatabaseInLocal:             "You must specify the database with local target type.",
	ErrCodeInvalidS3Uri:                    "Invalid S3 URI, example: s3://<bucket>/<path>, please check your S3 URI.",
	ErrCodeInvalidS3AccessKeyID:            "Invalid S3 access key id, only allowed the id start with 'AKIA' and length is 20.",
	ErrCodeExportNotSuccessful:             "Export is not successful, you can not execute this operation. Please check your export status.",
	ErrCodeExportNotLocal:                  "Export target is not local, you can not execute this operation. Please check your target type.",
	ErrCodeExportNoData:                    "Does not find any data, maybe the table you specify does not exist.",
	ErrCodeExportExpired:                   "The export has expired, please export again.",
	ErrCodeClusterNotFind:                  "Cluster not found, please check your cluster id.",
	ErrCodeExportUpdateStateAffectedNoRows: "Unable to update export status, maybe the export status is not running.",
	ErrCodeStorageLimitExceed:              "The storage limit of stashing area has been exceeded, please contact us if you need more quota.",
	ErrCodeInvalidCompression:              "Invalid compression type, only support GZIP, ZSTD, SNAPPY and NONE.",
	ErrCodeInvalidSnapshotTime:             "Invalid snapshot time, please set a snapshot time no later than the current time.",
	ErrCodeNotSupportSQLWithSQLQuery:       "Does not support exporting to sql file when filter with SQL statement.",
	ErrCodeInvalidTableFilter:              "Invalid filter patterns, see https://docs.pingcap.com/tidb/stable/table-filter to learn how to write filter patterns.",
	ErrCodeBothSQLAndTableExist:            "Both sql and table exist in the filter, please specify only one of them.",
	ErrCodeInvalidSQL:                      "Invalid SQL option, please check your SQL syntax.",
	ErrCodeInvalidWhere:                    "Invalid where option, please check your where syntax.",
	ErrCodeInvalidOrderby:                  "Invalid order by.",
	bizerr.ErrCodeRecordExist:              "The export name you're trying to use is already used. Please select an alternative name for your export.",
	ErrCodeEmptyS3RoleArn:                  "Empty S3 role arn.",
	ErrCodeEmptyS3Auth:                     "Empty S3 auth type, please specify one of the [ACCESS_KEY,ROLE_ARN].",
	ErrCodeGCSAuth:                         "Missing required GCS auth information.",
	ErrCodeAzBlobAuth:                      "Missing required Azure Blob auth information.",
	ErrCodeInvalidParquetCompression:       "Invalid parquet compression type, only support one of the [GZIP, SNAPPY, ZSTD, NONE].",
	ErrCodeSpecifyCompressionInParquet:     "You can not specify compression with parquet file type. Please use parquet_options.compression instead.",
	ErrCodeExceedMaxFileNumber:             "Exceed the maximum file number 100, please reduce the number of files.",
	ErrCodeExportFromThrottled:             "Export is not allowed for clusters that reach the Spending Limit and are experiencing throttling.",
	ErrCodeExportStorageLimitExceed:        "The storage limit of export has been exceeded(1T), please contact us and request the export.",
	ErrCodeEmptyExport:                     "Not allowed to export empty dataset, please check your data and try again.",
	ErrCodeExportConcurrencyLimitExceeded:  "This organization has reached the concurrent export limit.",
}

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

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