package mock

type WorkloadBROption struct {
	Label       string `json:"label"`
	BRValue     string `json:"br_value"`
	ImportValue string `json:"import_value"`
	// SQLParams is used when the prepare_method is sql_generate
	SQLParams map[string]string `json:"sql_params"`
	// Params is the params corresponding to the workload br datasets.
	// e.g. br dataset of 1000 warehouses should be run with 1000 warehouses traffic
	Params map[string]string `json:"params"`
}

// workloadBROptions defines the available options for different workloads.
// Pls set default value for each workload in TCMS workflow as fallback value
var workloadBROptions = map[int64][]WorkloadBROption{
	//TPCC
	180012: {
		{
			Label:       "1000 Warehouses, 102.1 GB",
			BRValue:     "s3://qa-workload-datasets/benchmark/tpcc-1k-with-clustered-index",
			ImportValue: "s3://qa-workload-datasets/benchmark/tpcc-1k-with-clustered-index-csv",
			SQLParams: map[string]string{
				"prepare_warehouses":  "1000",
				"prepare_method":      "sql_generate",
				"prepare_tpc_options": "",
			},
			Params: map[string]string{
				"warehouses":         "1000",
				"tpc_options":        "--ignore-error --conn-refresh-interval=10s",
				"prepare_br_options": "--check-requirements=false --concurrency=12 --tikv-max-restore-concurrency=16  --checksum=false",
			},
		},
		{
			Label:   "20000 Warehouses, 2.044 TB",
			BRValue: "s3://qa-workload-datasets/benchmark/tpcc20000",
			SQLParams: map[string]string{
				"prepare_warehouses":  "20000",
				"prepare_method":      "sql_generate",
				"prepare_tpc_options": "",
			},
			Params: map[string]string{
				"warehouses":         "20000",
				"tpc_options":        "--ignore-error --conn-refresh-interval=10s",
				"prepare_br_options": "--check-requirements=false --concurrency=12 --tikv-max-restore-concurrency=16  --checksum=false",
			},
		},
	},
	//HZBank
	120030: {
		{
			Label:   "274.2 GB",
			BRValue: "s3://qa-workload-datasets/benchmark/hzbank-new/",
		},
	},
	// Sysbench Read Write
	150039: {
		{
			Label:       "32 Tables, 10M Rows/Table, 81.28 GB",
			BRValue:     "s3://qa-workload-datasets/benchmark/sysbench-32-1000w-auto",
			ImportValue: "s3://qa-workload-datasets/benchmark/sysbench-32-1000w-auto-csv",
			SQLParams: map[string]string{
				"prepare_method":           "sql_generate",
				"prepare_sysbench_options": "--tables=32 --table-size=10000000 --mysql-ignore-errors=1062,2013,8028,8252,9002,9007 --auto-inc=false",
			},
			Params: map[string]string{
				"sysbench_options":   "--tables=32 --table-size=10000000 --mysql-ignore-errors=1062,2013,8028,8252,9002,9007 --auto-inc=false --reconnect=300",
				"prepare_br_options": "--check-requirements=false --concurrency=12 --tikv-max-restore-concurrency=16 --checksum=false",
			},
		},
		{
			Label:   "50 Tables, 100M Rows/Table, 1 TB",
			BRValue: "s3://qa-workload-datasets/benchmark/sysbench-50-100M-1T",
			SQLParams: map[string]string{
				"prepare_method":           "sql_generate",
				"prepare_sysbench_options": "--tables=50 --table-size=100000000 --mysql-ignore-errors=1062,2013,8028,8252,9002,9007 --auto-inc=false",
			},
			Params: map[string]string{
				"sysbench_options":   "--tables=50 --table-size=100000000 --mysql-ignore-errors=1062,2013,8028,8252,9002,9007 --auto-inc=false --reconnect=300",
				"prepare_br_options": "--check-requirements=false --concurrency=12 --tikv-max-restore-concurrency=16  --checksum=false",
			},
		},
	},
	// corebanking
	120040: {
		{
			Label:   "185.9 GB",
			BRValue: "s3://qa-workload-datasets/benchmark/corebanking-new/",
		},
	},
	// CH
	150040: {
		{
			Label:       "1000 Warehouses, 102.1 GB, No TiFlash",
			BRValue:     "s3://qa-workload-datasets/nfs/tiflash/ch-1k-flash0-collation/",
			ImportValue: "s3://qa-workload-datasets/nfs/tiflash/ch-1k-flash0-collation-csv",
			SQLParams: map[string]string{
				"prepare_warehouses": "1000",
				"prepare_method":     "sql_generate",
				"prepare_ch_options": " --analyze",
			},
			Params: map[string]string{
				"warehouses":         "1000",
				"tpc_options":        "--ignore-error --conn-refresh-interval=10s",
				"prepare_br_options": "--check-requirements=false --concurrency=12 --tikv-max-restore-concurrency=16  --checksum=false",
			},
		},
		{
			Label:   "2000 Warehouses, 251.5 GB, No TiFlash",
			BRValue: "s3://qa-workload-datasets/nfs/tiflash/ch-2k-flash0-collation/",
			SQLParams: map[string]string{
				"prepare_warehouses": "2000",
				"prepare_method":     "sql_generate",
				"prepare_ch_options": " --analyze",
			},
			Params: map[string]string{
				"warehouses":         "1000",
				"tpc_options":        "--ignore-error --conn-refresh-interval=10s",
				"prepare_br_options": "--check-requirements=false --concurrency=12 --tikv-max-restore-concurrency=16  --checksum=false",
			},
		},
		{
			Label:   "2000 Warehouses, 251.5 GB, With TiFlash",
			BRValue: "s3://qa-workload-datasets/nfs/tiflash/ch-2k-flash2-collation/",
			SQLParams: map[string]string{
				"prepare_warehouses": "2000",
				"prepare_method":     "sql_generate",
				"prepare_ch_options": "--tiflash-replica 2 --analyze",
			},
			Params: map[string]string{
				"warehouses":         "2000",
				"tpc_options":        "--ignore-error --conn-refresh-interval=10s",
				"prepare_br_options": "--check-requirements=false --concurrency=12 --tikv-max-restore-concurrency=16  --checksum=false",
			},
		},
		{
			Label:   "5000 Warehouses, With TiFlash",
			BRValue: "s3://qa-workload-datasets/benchmark/ch-5k-tiflash/",
			SQLParams: map[string]string{
				"prepare_warehouses": "5000",
				"prepare_method":     "sql_generate",
				"prepare_ch_options": "--tiflash-replica 2 --analyze",
			},
			Params: map[string]string{
				"warehouses":         "5000",
				"tpc_options":        "--ignore-error --conn-refresh-interval=10s",
				"prepare_br_options": "--check-requirements=false --concurrency=12 --tikv-max-restore-concurrency=16  --checksum=false",
			},
		},
	},
	// Sysbench Point Select
	150038: {
		{
			Label:       "32 Tables, 10M Rows/Table, 81.28 GB",
			BRValue:     "s3://qa-workload-datasets/benchmark/sysbench-32-1000w-auto",
			ImportValue: "s3://qa-workload-datasets/benchmark/sysbench-32-1000w-auto-csv",
			SQLParams: map[string]string{
				"prepare_method":           "sql_generate",
				"prepare_sysbench_options": "--tables=32 --table-size=10000000 --mysql-ignore-errors=1062,2013,8028,9002,9007 --auto-inc=false",
			},
			Params: map[string]string{
				"sysbench_options": "--tables=32 --table-size=10000000 --mysql-ignore-errors=1062,2013,8028,9002,9007 --auto-inc=false --reconnect=300",
			},
		},
		{
			Label:   "50 Tables, 100M Rows/Table, 1 TB",
			BRValue: "s3://qa-workload-datasets/benchmark/sysbench-50-100M-1T",
			SQLParams: map[string]string{
				"prepare_method":           "sql_generate",
				"prepare_sysbench_options": "--tables=50 --table-size=100000000 --mysql-ignore-errors=1062,2013,8028,9002,9007 --auto-inc=false",
			},
			Params: map[string]string{
				"sysbench_options": "--tables=50 --table-size=100000000 --mysql-ignore-errors=1062,2013,8028,9002,9007 --auto-inc=false --reconnect=300",
			},
		},
	},
	// million tables
	180013: {
		{
			Label:   "500K Databases, 1M Tables, 1.47 TB",
			BRValue: "s3://qa-workload-datasets/forge_million_tables/",
			Params: map[string]string{
				"prepare_br_options": "--checksum=false",
			},
		},
		{
			Label:   "250K Databases, 0.5M Tables, 865.2 GB",
			BRValue: "s3://qa-workload-datasets/forge_500k_tables/",
			Params: map[string]string{
				"prepare_br_options": "--checksum=false",
			},
		},
	},
	// ue-chain
	120061: {
		{
			Label:   "63.33 GB",
			BRValue: "s3://qa-workload-datasets/ue-chain/ue-chain-63.33GB/",
		},
	},
	// saas app05
	180041: {
		{
			Label: "45.36 GB Data Size, 77.97 GB Index Size",
			// This is actually the csv directory, we only have it for this workload.
			BRValue:     "s3://qa-workload-datasets/wetech_saas05_7980_20_percent_new/",
			ImportValue: "s3://qa-workload-datasets/wetech_saas05_7980_20_percent_new/",
			Params: map[string]string{
				"db_name": "dh_app_7980",
			},
		},
		{
			Label: "45.36 GB Data Size, 71.63 GB Index Size(No User Index)",
			// This is actually the csv directory, we only have it for this workload.
			BRValue:     "s3://qa-workload-datasets/wetech_saas05_20_percent_rate10_desensitized/",
			ImportValue: "s3://qa-workload-datasets/wetech_saas05_20_percent_rate10_desensitized/",
			Params: map[string]string{
				"db_name":          "game_1",
				"sysbench_options": "--db_name_prefix=game --db_index_start=1 --db_index_end=1 --insert_ratio=10 --update_ratio=10 --mysql-ignore-errors=1062,2013,8028,9002,9007,1213 --rate=10",
			},
		},
	},
	// prod-dc
	120082: {
		{
			Label:       "216.05 GB Data Length, 78.17 GB Index Length",
			BRValue:     "s3://qa-workload-datasets/benchmark/bingo_prod_dc/dumpling/c66_dc_1t",
			ImportValue: "s3://qa-workload-datasets/benchmark/bingo_prod_dc/dumpling/c66_dc_1t",
			Params: map[string]string{
				"db_name":        "c66_dc",
				"prepare_method": "import",
			},
		},
	},
	// prod-mms
	150093: {
		{
			Label:       "2.06 TB Data Length, 1.88 TB Index Length",
			BRValue:     "s3://qa-workload-datasets/benchmark/bingo_mms_csv/",
			ImportValue: "s3://qa-workload-datasets/benchmark/bingo_mms_csv/",
			Params: map[string]string{
				"prepare_method": "import",
			},
		},
	},
	// prod-jili
	120083: {
		{
			Label:       "108.79 GB Data Length, 27.72 GB Index Length",
			BRValue:     "s3://qa-workload-datasets/benchmark/bingo_prod_jili_1t_csv",
			ImportValue: "s3://qa-workload-datasets/benchmark/bingo_prod_jili_1t_csv",
			Params: map[string]string{
				"prepare_method": "import",
			},
		},
	},
}

// WorkloadBRLabels returns a slice of labels for a given workload name.
func WorkloadBRLabels(workloadID int64) []string {
	// Check if workloadName exists in workloadBROptions
	if options, ok := workloadBROptions[workloadID]; ok {
		// Create a slice to store the labels
		ret := make([]string, len(options))
		// Iterate over the options and extract labels
		for i, option := range options {
			ret[i] = option.Label
		}
		return ret
	}

	// Return nil if workloadName is not found
	return nil
}

// workloadBRValue returns the value (S3 path) for a given workload name and label.
func workloadBRValue(workloadID int64, label string) string {
	if options, ok := workloadBROptions[workloadID]; ok {
		for _, option := range options {
			if option.Label == label {
				return option.BRValue
			}
		}
	}

	return ""
}

func workloadBRParams(workloadID int64, label string) map[string]string {
	if options, ok := workloadBROptions[workloadID]; ok {
		for _, option := range options {
			if option.Label == label {
				return option.Params
			}
		}
	}

	return nil
}

func workloadProdPrepareParams(workloadID int64, label string) map[string]string {
	if options, ok := workloadBROptions[workloadID]; ok {
		for _, option := range options {
			if option.Label == label {
				if option.ImportValue != "" {
					return map[string]string{
						"prepare_method": "import",
						// This is not a typo, it's the param name in tcms, and will be transformed to storage_url when parsed to tiworkload
						"br_storage_url": option.ImportValue,
					}
				}
				return option.SQLParams
			}
		}
	}

	return nil
}

func isWorkloadSupportSqlGenerate(workloadID int64) bool {
	if opt, ok := workloadBROptions[workloadID]; ok {
		for _, option := range opt {
			if option.SQLParams != nil {
				return true
			}
		}
	}

	return false
}

func isWorkloadSupportImport(workloadID int64) bool {
	if opt, ok := workloadBROptions[workloadID]; ok {
		for _, option := range opt {
			if option.ImportValue != "" {
				return true
			}
		}
	}

	return false
}
