package alicloud

import (
	"fmt"
	"os"
	"strings"
	"testing"

	"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"

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

func TestAccAliCloudWafv3DomainDataSource(t *testing.T) {
	rand := acctest.RandIntRange(1000000, 99999999)
	// cn region needs icp domain, and using intl region instead
	checkoutSupportedRegions(t, true, connectivity.WAFSupportRegions)
	idsConf := dataSourceTestAccConfig{
		existConfig: testAccCheckAliCloudWafv3DomainSourceConfig(rand, map[string]string{
			"ids": `["${alicloud_wafv3_domain.default.id}"]`,
		}),
		fakeConfig: testAccCheckAliCloudWafv3DomainSourceConfig(rand, map[string]string{
			"ids": `["${alicloud_wafv3_domain.default.id}_fake"]`,
		}),
	}
	DomainConf := dataSourceTestAccConfig{
		existConfig: testAccCheckAliCloudWafv3DomainSourceConfig(rand, map[string]string{
			"domain": `"${alicloud_wafv3_domain.default.domain}"`,
		}),
		fakeConfig: testAccCheckAliCloudWafv3DomainSourceConfig(rand, map[string]string{
			"domain": `"${alicloud_wafv3_domain.default.domain}_fake"`,
		}),
	}
	backendConf := dataSourceTestAccConfig{
		existConfig: testAccCheckAliCloudWafv3DomainSourceConfig(rand, map[string]string{
			"backend": `"1.1.1.1"`,
		}),
		fakeConfig: testAccCheckAliCloudWafv3DomainSourceConfig(rand, map[string]string{
			"backend": `"1.1.1.2"`,
		}),
	}
	allConf := dataSourceTestAccConfig{
		existConfig: testAccCheckAliCloudWafv3DomainSourceConfig(rand, map[string]string{
			"ids":     `["${alicloud_wafv3_domain.default.id}"]`,
			"domain":  `"${alicloud_wafv3_domain.default.domain}"`,
			"backend": `"1.1.1.1"`,
		}),
		fakeConfig: testAccCheckAliCloudWafv3DomainSourceConfig(rand, map[string]string{
			"ids":     `["${alicloud_wafv3_domain.default.id}_fake"]`,
			"domain":  `"${alicloud_wafv3_domain.default.domain}_fake"`,
			"backend": `"1.1.1.2"`,
		}),
	}

	preCheck := func() {
		testAccPreCheck(t)
		testAccPreCheckForCleanUpInstances(t, string(connectivity.APSouthEast1), "waf", "waf", "waf", "waf")
	}

	Wafv3DomainCheckInfo.dataSourceTestCheckWithPreCheck(t, rand, preCheck, idsConf, DomainConf, backendConf, allConf)
}

var existWafv3DomainMapFunc = func(rand int) map[string]string {
	return map[string]string{
		"ids.#":            "1",
		"domains.#":        "1",
		"domains.0.id":     CHECKSET,
		"domains.0.domain": CHECKSET,
		"domains.0.resource_manager_resource_group_id": CHECKSET,
		"domains.0.status":                             CHECKSET,
		"domains.0.cname":                              CHECKSET,
		"domains.0.listen.#":                           "1",
		"domains.0.listen.0.cert_id":                   CHECKSET,
		"domains.0.listen.0.cipher_suite":              "99",
		"domains.0.listen.0.enable_tlsv3":              "true",
		"domains.0.listen.0.exclusive_ip":              "false",
		"domains.0.listen.0.focus_https":               "false",
		"domains.0.listen.0.http2_enabled":             "true",
		"domains.0.listen.0.ipv6_enabled":              "false",
		"domains.0.listen.0.protection_resource":       "share",
		"domains.0.listen.0.tls_version":               "tlsv1",
		"domains.0.listen.0.xff_header_mode":           "2",
		"domains.0.listen.0.http_ports.#":              "1",
		"domains.0.listen.0.https_ports.#":             "1",
		"domains.0.listen.0.custom_ciphers.#":          "1",
		"domains.0.listen.0.xff_headers.#":             "2",
		"domains.0.redirect.#":                         "1",
		"domains.0.redirect.0.loadbalance":             "iphash",
		"domains.0.redirect.0.focus_http_backend":      "false",
		"domains.0.redirect.0.keepalive":               "true",
		"domains.0.redirect.0.keepalive_requests":      "80",
		"domains.0.redirect.0.retry":                   "true",
		"domains.0.redirect.0.sni_enabled":             "true",
		"domains.0.redirect.0.sni_host":                "www.aliyun.com",
		"domains.0.redirect.0.connect_timeout":         "30",
		"domains.0.redirect.0.keepalive_timeout":       "30",
		"domains.0.redirect.0.read_timeout":            "30",
		"domains.0.redirect.0.write_timeout":           "30",
		"domains.0.redirect.0.backends.#":              "1",
		"domains.0.redirect.0.request_headers.#":       "1",
		"domains.0.redirect.0.request_headers.0.key":   CHECKSET,
		"domains.0.redirect.0.request_headers.0.value": CHECKSET,
	}
}

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

var Wafv3DomainCheckInfo = dataSourceAttr{
	resourceId:   "data.alicloud_wafv3_domains.default",
	existMapFunc: existWafv3DomainMapFunc,
	fakeMapFunc:  fakeWafv3DomainMapFunc,
}

func testAccCheckAliCloudWafv3DomainSourceConfig(rand int, attrMap map[string]string) string {
	var pairs []string
	for k, v := range attrMap {
		pairs = append(pairs, k+" = "+v)
	}
	casRegion := "cn-hangzhou"
	if strings.ToLower(os.Getenv("ALIBABA_CLOUD_ACCOUNT_TYPE")) == "international" {
		casRegion = "ap-southeast-1"
	}
	config := fmt.Sprintf(`
	variable "name" {
  		default = "tftest%d.tftest.top"
	}

	resource "alicloud_wafv3_instance" "default" {
	}

	resource "alicloud_ssl_certificates_service_certificate" "default" {
  		certificate_name = var.name
  		cert             = <<EOF
-----BEGIN CERTIFICATE-----
MIIDeDCCAmCgAwIBAgIEN3ZT6zANBgkqhkiG9w0BAQsFADBVMQswCQYDVQQGEwJD
TjEVMBMGA1UEAwwMKi50ZnRlc3QudG9wMRAwDgYDVQQIDAdCZWlKaW5nMRAwDgYD
VQQHDAdCZWlKaW5nMQswCQYDVQQKDAJBQTAeFw0yMzA4MjgwNjQ5NDNaFw0yNTA4
MjcwNjQ5NDNaMFUxCzAJBgNVBAYTAkNOMRUwEwYDVQQDDAwqLnRmdGVzdC50b3Ax
EDAOBgNVBAgMB0JlaUppbmcxEDAOBgNVBAcMB0JlaUppbmcxCzAJBgNVBAoMAkFB
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzkk9NJUH7PLSQK4RRrGQ
Y5dVsftkhnKh9HhI6yrnlowWIDPS1PZHOU/5gQ7xPUPGdKQV5S7x8wROnAaXEimx
N4GdQw25pGhRJvlwme9fzJJiSe6lG49NCxmuBiEdJAzPKaTPpK1cG1f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NFh87tpPlyymWMOLTnf3Kkcfszlfp45idOBGCu46V9NDVbppT
2UmrSIR/H5dbTXsNcAlt/hhlpeInjhkU1VqmH10CgYEA7Kk+QhWq705SczpWjm5J
9kHqfFzJLwAWNBduiia0WypgPhLe/4wT1rYQkBtKMVKrgFo7Cvi4YKlrtlDnXyeU
CIFqfEL5NriQelqrFsvgHsmD+MpvDoSWm5C8IrTubtlNyWUzXSVT4OIwzPobzPqG
LILJ+e7bLw8RrM0HfgFnl8c=
-----END PRIVATE KEY-----
EOF
	}

	resource "alicloud_wafv3_domain" "default" {
  		instance_id = alicloud_wafv3_instance.default.id
  		domain      = var.name
  		access_type = "share"
  		listen {
    		http_ports          = [80]
    		https_ports         = [443]
    		cert_id             = local.certificate_id
    		cipher_suite        = 99
    		xff_header_mode     = 2
    		protection_resource = "share"
			tls_version         = "tlsv1"
			enable_tlsv3        = true
			http2_enabled       = true
			custom_ciphers      = ["ECDHE-ECDSA-AES128-GCM-SHA256"]
			focus_https         = false
			ipv6_enabled        = false
			exclusive_ip        = false
			xff_headers         = ["A", "B"]
  		}
  		redirect {
			backends           = ["1.1.1.1"]
			loadbalance        = "iphash"
			sni_enabled        = true
			sni_host           = "www.aliyun.com"
			focus_http_backend = false
			keepalive          = true
			retry              = true
			keepalive_requests = 80
			keepalive_timeout  = 30
			connect_timeout    = 30
			read_timeout       = 30
			write_timeout      = 30
    		request_headers {
      			key   = "A"
      			value = "B"
    		}
  		}

	}

	locals {
  		certificate_id = join("-", [alicloud_ssl_certificates_service_certificate.default.id, "%s"])
	}

	data "alicloud_wafv3_domains" "default" {
  		instance_id    = alicloud_wafv3_instance.default.id
  		enable_details = true
		%s
	}
`, rand, casRegion, strings.Join(pairs, "\n   "))
	return config
}
