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

		updateMalwarePolicyActionsResponse := appsec.UpdateMalwarePolicyActionsResponse{}
		err := json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResMalwarePolicyActions/MalwarePolicyActions.json"), &updateMalwarePolicyActionsResponse)
		require.NoError(t, err)
		getMalwarePolicyActionsResponse := appsec.GetMalwarePolicyActionsResponse{}
		err = json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResMalwarePolicyActions/MalwarePolicyActions.json"), &getMalwarePolicyActionsResponse)
		require.NoError(t, err)

		client.On("GetMalwarePolicyActions",
			testutils.MockContext,
			appsec.GetMalwarePolicyActionsRequest{ConfigID: 43253, Version: 7, PolicyID: "AAAA_81230"},
		).Return(&getMalwarePolicyActionsResponse, nil)

		client.On("UpdateMalwarePolicyActions",
			testutils.MockContext,
			appsec.UpdateMalwarePolicyActionsRequest{ConfigID: 43253, Version: 7, PolicyID: "AAAA_81230"},
		).Return(&updateMalwarePolicyActionsResponse, nil)

		useClient(client, func() {
			resource.Test(t, resource.TestCase{
				IsUnitTest:               false,
				ProtoV6ProviderFactories: testutils.NewProtoV6ProviderFactory(NewSubprovider()),
				Steps: []resource.TestStep{
					{
						Config: testutils.LoadFixtureString(t, "testdata/TestResMalwarePolicyActions/match_by_id.tf"),
						Check: resource.ComposeAggregateTestCheckFunc(
							resource.TestCheckResourceAttr("akamai_appsec_malware_policy_actions.test", "id", "135355"),
						),
					},

					{
						Config: testutils.LoadFixtureString(t, "testdata/TestResMalwarePolicyActions/update_by_id.tf"),
						Check: resource.ComposeTestCheckFunc(
							resource.TestCheckResourceAttr("akamai_appsec_malware_policy_actions.test", "id", "135355"),
						),
					},
				},
			})
		})

		client.AssertExpectations(t)
	})

}
