package css

import (
	"testing"

	"github.com/sbercloud-terraform/terraform-provider-sbercloud/sbercloud/acceptance"

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

func TestAccCssFlavorsDataSource_basic(t *testing.T) {
	var (
		typeFilter    = acceptance.InitDataSourceCheck("data.sbercloud_css_flavors.type_filter")
		versionFilter = acceptance.InitDataSourceCheck("data.sbercloud_css_flavors.version_filter")
		vcpusFilter   = acceptance.InitDataSourceCheck("data.sbercloud_css_flavors.vcpus_filter")
		memoryFilter  = acceptance.InitDataSourceCheck("data.sbercloud_css_flavors.memory_filter")
	)

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:          func() { acceptance.TestAccPreCheck(t) },
		ProviderFactories: acceptance.TestAccProviderFactories,
		Steps: []resource.TestStep{
			{
				Config: testAccDataSourceCssFlavors_basic,
				Check: resource.ComposeTestCheckFunc(
					typeFilter.CheckResourceExists(),
					resource.TestCheckOutput("is_type_filter_useful", "true"),
					versionFilter.CheckResourceExists(),
					resource.TestCheckOutput("is_version_filter_useful", "true"),
					vcpusFilter.CheckResourceExists(),
					resource.TestCheckOutput("is_vcpus_filter_useful", "true"),
					memoryFilter.CheckResourceExists(),
					resource.TestCheckOutput("is_memory_filter_useful", "true"),
				),
			},
		},
	})
}

const testAccDataSourceCssFlavors_basic = `

data "sbercloud_css_flavors" "type_filter" {
  type = "ess"
}

output "is_type_filter_useful" {
  value = !contains([for v in data.sbercloud_css_flavors.type_filter.flavors[*].type : v == "ess"], "false")
}

data "sbercloud_css_flavors" "version_filter" {
  version = "7.9.3"
}

output "is_version_filter_useful" {
  value = !contains([for v in data.sbercloud_css_flavors.version_filter.flavors[*].version : v == "7.9.3"], "false")
}

data "sbercloud_css_flavors" "vcpus_filter" {
  vcpus = 32
}

output "is_vcpus_filter_useful" {
  value = !contains([for v in data.sbercloud_css_flavors.vcpus_filter.flavors[*].vcpus : v == 32], "false")
}

data "sbercloud_css_flavors" "memory_filter" {
  memory = 256
}

output "is_memory_filter_useful" {
  value = !contains([for v in data.sbercloud_css_flavors.memory_filter.flavors[*].memory : v == 256], "false")
}
`

func TestAccCssFlavorsDataSource_all(t *testing.T) {
	dataSourceName := "data.sbercloud_css_flavors.test"

	dc := acceptance.InitDataSourceCheck(dataSourceName)

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:          func() { acceptance.TestAccPreCheck(t) },
		ProviderFactories: acceptance.TestAccProviderFactories,
		Steps: []resource.TestStep{
			{
				Config: testAccDataSourceCssFlavors_all,
				Check: resource.ComposeTestCheckFunc(
					dc.CheckResourceExists(),
					resource.TestCheckResourceAttr(dataSourceName, "type", "ess"),
					resource.TestCheckResourceAttr(dataSourceName, "version", "7.9.3"),
					resource.TestCheckResourceAttrSet(dataSourceName, "id"),
					resource.TestCheckResourceAttr(dataSourceName, "region", "ru-moscow-1"),
					resource.TestCheckResourceAttr(dataSourceName, "name", "ess.spec-ds.8xlarge.8"),
					resource.TestCheckResourceAttr(dataSourceName, "memory", "256"),
					resource.TestCheckResourceAttr(dataSourceName, "vcpus", "32"),
				),
			},
		},
	})
}

const testAccDataSourceCssFlavors_all = `
data "sbercloud_css_flavors" "test" {
  type    = "ess"
  version = "7.9.3"
  vcpus   = 32
  memory  = 256
  region  = "ru-moscow-1"
  name    = "ess.spec-ds.8xlarge.8"
}
`
