package appsec

import (
	"encoding/json"
	"testing"

	"github.com/akamai/AkamaiOPEN-edgegrid-golang/v12/pkg/appsec"
	"github.com/akamai/terraform-provider-akamai/v9/pkg/common/testutils"
	"github.com/hashicorp/terraform-plugin-testing/helper/resource"
	"github.com/stretchr/testify/require"
)

func TestAkamaiMalwarePolicies_data_basic(t *testing.T) {
	t.Run("match by MalwarePolicies ID", func(t *testing.T) {
		client := &appsec.Mock{}

		config := appsec.GetConfigurationResponse{}
		err := json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResConfiguration/LatestConfiguration.json"), &config)
		require.NoError(t, err)

		client.On("GetConfiguration",
			testutils.MockContext,
			appsec.GetConfigurationRequest{ConfigID: 43253},
		).Return(&config, nil)

		getMalwarePoliciesResponse := appsec.MalwarePoliciesResponse{}
		err = json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestDSMalwarePolicies/MalwarePolicies.json"), &getMalwarePoliciesResponse)
		require.NoError(t, err)

		client.On("GetMalwarePolicies",
			testutils.MockContext,
			appsec.GetMalwarePoliciesRequest{ConfigID: 43253, ConfigVersion: 7},
		).Return(&getMalwarePoliciesResponse, nil)

		useClient(client, func() {
			resource.Test(t, resource.TestCase{
				IsUnitTest:               true,
				ProtoV6ProviderFactories: testutils.NewProtoV6ProviderFactory(NewSubprovider()),
				Steps: []resource.TestStep{
					{
						Config: testutils.LoadFixtureString(t, "testdata/TestDSMalwarePolicies/match_by_id.tf"),
						Check: resource.ComposeAggregateTestCheckFunc(
							resource.TestCheckResourceAttr("data.akamai_appsec_malware_policies.test", "id", "43253"),
						),
					},
				},
			})
		})

		client.AssertExpectations(t)
	})

}

func TestAkamaiMalwarePolicies_data_single_policy(t *testing.T) {
	t.Run("match by MalwarePolicies ID single policy", func(t *testing.T) {
		client := &appsec.Mock{}

		config := appsec.GetConfigurationResponse{}
		err := json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResConfiguration/LatestConfiguration.json"), &config)
		require.NoError(t, err)

		client.On("GetConfiguration",
			testutils.MockContext,
			appsec.GetConfigurationRequest{ConfigID: 43253},
		).Return(&config, nil)

		getMalwarePolicyResponse := appsec.MalwarePolicyResponse{}
		err = json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestDSMalwarePolicies/MalwarePolicy.json"), &getMalwarePolicyResponse)
		require.NoError(t, err)

		client.On("GetMalwarePolicy",
			testutils.MockContext,
			appsec.GetMalwarePolicyRequest{ConfigID: 43253, ConfigVersion: 7, MalwarePolicyID: 999},
		).Return(&getMalwarePolicyResponse, nil)

		useClient(client, func() {
			resource.Test(t, resource.TestCase{
				IsUnitTest:               true,
				ProtoV6ProviderFactories: testutils.NewProtoV6ProviderFactory(NewSubprovider()),
				Steps: []resource.TestStep{
					{
						Config: testutils.LoadFixtureString(t, "testdata/TestDSMalwarePolicies/match_by_id_single_policy.tf"),
						Check: resource.ComposeAggregateTestCheckFunc(
							resource.TestCheckResourceAttr("data.akamai_appsec_malware_policies.test", "id", "43253:999"),
							resource.TestCheckResourceAttr("data.akamai_appsec_malware_policies.test", "malware_policy_id", "999"),
						),
					},
				},
			})
		})

		client.AssertExpectations(t)
	})

}
