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 TestAkamaiMalwarePolicy_res_basic(t *testing.T) {
	t.Run("match by MalwarePolicy ID", func(t *testing.T) {
		client := &appsec.Mock{}

		// Messages for test step 1
		configResponse := appsec.GetConfigurationResponse{}
		err := json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResConfiguration/LatestConfiguration.json"), &configResponse)
		require.NoError(t, err)
		createBody := appsec.MalwarePolicyBody{}
		err = json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResMalwarePolicy/CreateMalwarePolicy.json"), &createBody)
		require.NoError(t, err)
		createResponse := appsec.MalwarePolicyResponse{}
		err = json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResMalwarePolicy/MalwarePolicy.json"), &createResponse)
		require.NoError(t, err)
		getResponse := appsec.MalwarePolicyResponse{}
		err = json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResMalwarePolicy/MalwarePolicy.json"), &getResponse)
		require.NoError(t, err)

		// Calls made during test step 1
		client.On("GetConfiguration", testutils.MockContext,
			appsec.GetConfigurationRequest{ConfigID: 43253},
		).Return(&configResponse, nil)
		client.On("CreateMalwarePolicy",
			testutils.MockContext,
			appsec.CreateMalwarePolicyRequest{ConfigID: 43253, ConfigVersion: 7, Policy: &createBody},
		).Return(&createResponse, nil)
		client.On("GetMalwarePolicy", // during Create
			testutils.MockContext,
			appsec.GetMalwarePolicyRequest{ConfigID: 43253, ConfigVersion: 7, MalwarePolicyID: 134644},
		).Return(&getResponse, nil).Once()
		client.On("GetMalwarePolicy", // after Create
			testutils.MockContext,
			appsec.GetMalwarePolicyRequest{ConfigID: 43253, ConfigVersion: 7, MalwarePolicyID: 134644},
		).Return(&getResponse, nil).Once()

		// Messages for test step 2
		updateBody := appsec.MalwarePolicyBody{}
		err = json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResMalwarePolicy/UpdateMalwarePolicy.json"), &updateBody)
		require.NoError(t, err)
		updateResponse := appsec.MalwarePolicyResponse{}
		err = json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResMalwarePolicy/MalwarePolicyUpdated.json"), &updateResponse)
		require.NoError(t, err)
		getUpdatedResponse := appsec.MalwarePolicyResponse{}
		err = json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResMalwarePolicy/MalwarePolicyUpdated.json"), &getUpdatedResponse)
		require.NoError(t, err)

		// Calls made during test step 2
		client.On("GetMalwarePolicy",
			testutils.MockContext,
			appsec.GetMalwarePolicyRequest{ConfigID: 43253, ConfigVersion: 7, MalwarePolicyID: 134644},
		).Return(&getUpdatedResponse, nil)

		// Calls to Remove()
		client.On("RemoveMalwarePolicy",
			testutils.MockContext,
			appsec.RemoveMalwarePolicyRequest{ConfigID: 43253, ConfigVersion: 7, MalwarePolicyID: 134644},
		).Return(nil)
		useClient(client, func() {
			resource.Test(t, resource.TestCase{
				IsUnitTest:               true,
				ProtoV6ProviderFactories: testutils.NewProtoV6ProviderFactory(NewSubprovider()),
				Steps: []resource.TestStep{
					{
						Config: testutils.LoadFixtureString(t, "testdata/TestResMalwarePolicy/match_by_id.tf"),
						Check: resource.ComposeAggregateTestCheckFunc(
							resource.TestCheckResourceAttr("akamai_appsec_malware_policy.test", "id", "43253:134644"),
						),
					},
					{
						Config: testutils.LoadFixtureString(t, "testdata/TestResMalwarePolicy/update_by_id.tf"),
						Check: resource.ComposeAggregateTestCheckFunc(
							resource.TestCheckResourceAttr("akamai_appsec_malware_policy.test", "id", "43253:134644"),
						),
					},
				},
			})
		})

		client.AssertExpectations(t)
	})
}
