package alicloud

import (
	"fmt"
	"strings"
	"testing"

	"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)

/*
   Because 'Slot' is generated by the customer using SQL in the database, it cannot be automatically tested online.
*/

func SkipTestAccAlicloudRdsSlotsDataSource(t *testing.T) {
	rand := acctest.RandInt()
	resourceId := "data.alicloud_rds_slots.default"
	rdsSlotsConfig := dataSourceTestAccConfig{
		existConfig: dataSourceRdsSlotsConfigDependence(rand, map[string]string{
			"db_instance_id": `"${alicloud_db_instance.default.id}"`,
		}),
	}
	allConfig := dataSourceTestAccConfig{
		existConfig: dataSourceRdsSlotsConfigDependence(rand, map[string]string{
			"db_instance_id":    `"${alicloud_db_instance.default.id}"`,
			"resource_group_id": `"${alicloud_db_instance.default.resource_group_id}"`,
		}),
		fakeConfig: dataSourceRdsSlotsConfigDependence(rand, map[string]string{
			"db_instance_id":    `"${alicloud_db_instance.default.id}"`,
			"resource_group_id": `"${alicloud_db_instance.default.resource_group_id}_fake"`,
		}),
	}

	var existAlicloudRdsSlotsMapFunc = func(rand int) map[string]string {
		return map[string]string{
			"slots.#":             "1",
			"slots.0.slot_name":   CHECKSET,
			"slots.0.plugin":      CHECKSET,
			"slots.0.slot_type":   CHECKSET,
			"slots.0.database":    CHECKSET,
			"slots.0.temporary":   CHECKSET,
			"slots.0.slot_status": CHECKSET,
			"slots.0.wal_delay":   CHECKSET,
		}
	}

	var fakeAlicloudRdsSlotsMapFunc = func(rand int) map[string]string {
		return map[string]string{
			"slots.#": "0",
		}
	}

	var alicloudRdsSlotsCheckInfo = dataSourceAttr{
		resourceId:   resourceId,
		existMapFunc: existAlicloudRdsSlotsMapFunc,
		fakeMapFunc:  fakeAlicloudRdsSlotsMapFunc,
	}

	alicloudRdsSlotsCheckInfo.dataSourceTestCheck(t, rand, rdsSlotsConfig, allConfig)
}

func dataSourceRdsSlotsConfigDependence(rand int, attrMap map[string]string) string {
	var pairs []string
	for k, v := range attrMap {
		pairs = append(pairs, k+" = "+v)
	}
	return fmt.Sprintf(`
variable "name" {
	default = "pg_test_slots"
}
data "alicloud_db_zones" "default"{
	engine = "PostgreSQL"
	engine_version = "13.0"
	instance_charge_type = "PostPaid"
	category = "HighAvailability"
 	db_instance_storage_type = "cloud_essd"
}

data "alicloud_db_instance_classes" "default" {
    zone_id = data.alicloud_db_zones.default.zones.0.id
	engine = "PostgreSQL"
	engine_version = "13.0"
    category = "HighAvailability"
 	db_instance_storage_type = "cloud_essd"
	instance_charge_type = "PostPaid"
}

data "alicloud_vpcs" "default" {
    name_regex = "^default-NODELETING$"
}
data "alicloud_vswitches" "default" {
  vpc_id = data.alicloud_vpcs.default.ids.0
  zone_id = data.alicloud_db_zones.default.zones.0.id
}

resource "alicloud_db_instance" "default" {
    engine = "PostgreSQL"
	engine_version = "13.0"
 	db_instance_storage_type = "cloud_essd"
	instance_type = data.alicloud_db_instance_classes.default.instance_classes.0.instance_class
	instance_storage = data.alicloud_db_instance_classes.default.instance_classes.0.storage_range.min
	vswitch_id = data.alicloud_vswitches.default.ids.0
	instance_name = var.name
}

data "alicloud_rds_slots" "default" {	
  %s
}`, strings.Join(pairs, "\n"))
}
