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 TestAkamaiMalwarePolicyAction_res_basic(t *testing.T) {
	t.Run("match by MalwarePolicyAction 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)

		actionAfterCreate := appsec.UpdateMalwarePolicyActionResponse{}
		err = json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResMalwarePolicyAction/ActionAfterCreate.json"), &actionAfterCreate)
		require.NoError(t, err)

		allActionsAfterCreate := appsec.GetMalwarePolicyActionsResponse{}
		err = json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResMalwarePolicyAction/AllActionsAfterCreate.json"), &allActionsAfterCreate)
		require.NoError(t, err)

		actionAfterUpdate := appsec.UpdateMalwarePolicyActionResponse{}
		err = json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResMalwarePolicyAction/ActionAfterUpdate.json"), &actionAfterUpdate)
		require.NoError(t, err)

		allActionsAfterUpdate := appsec.GetMalwarePolicyActionsResponse{}
		err = json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResMalwarePolicyAction/AllActionsAfterUpdate.json"), &allActionsAfterUpdate)
		require.NoError(t, err)

		actionAfterDelete := appsec.UpdateMalwarePolicyActionResponse{}
		err = json.Unmarshal(testutils.LoadFixtureBytes(t, "testdata/TestResMalwarePolicyAction/ActionAfterDelete.json"), &actionAfterDelete)
		require.NoError(t, err)

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

		// Create ("none", "none")
		client.On("UpdateMalwarePolicyAction",
			testutils.MockContext,
			appsec.UpdateMalwarePolicyActionRequest{ConfigID: 43253, Version: 7, PolicyID: "AAAA_81230", MalwarePolicyID: 135355, Action: "none", UnscannedAction: "none"},
		).Return(&actionAfterCreate, nil).Once()

		// Read called from Create, returns ("none, "none") for this policy
		client.On("GetMalwarePolicyActions",
			testutils.MockContext,
			appsec.GetMalwarePolicyActionsRequest{ConfigID: 43253, Version: 7, PolicyID: "AAAA_81230"},
		).Return(&allActionsAfterCreate, nil).Once()

		// Read called for TestStep 1, returns ("none", "none") for this policy
		client.On("GetMalwarePolicyActions",
			testutils.MockContext,
			appsec.GetMalwarePolicyActionsRequest{ConfigID: 43253, Version: 7, PolicyID: "AAAA_81230"},
		).Return(&allActionsAfterCreate, nil).Once()

		// Read called by TF to determine whether Update will be called (diff check), returns ("none", "none") for this policy
		client.On("GetMalwarePolicyActions",
			testutils.MockContext,
			appsec.GetMalwarePolicyActionsRequest{ConfigID: 43253, Version: 7, PolicyID: "AAAA_81230"},
		).Return(&allActionsAfterCreate, nil).Once()

		// Update with ("alert", "deny")
		client.On("UpdateMalwarePolicyAction",
			testutils.MockContext,
			appsec.UpdateMalwarePolicyActionRequest{ConfigID: 43253, Version: 7, PolicyID: "AAAA_81230", MalwarePolicyID: 135355, Action: "alert", UnscannedAction: "deny"},
		).Return(&actionAfterUpdate, nil).Once()

		// Read called from Update, returns ("alert", "deny") for this policy
		client.On("GetMalwarePolicyActions",
			testutils.MockContext,
			appsec.GetMalwarePolicyActionsRequest{ConfigID: 43253, Version: 7, PolicyID: "AAAA_81230"},
		).Return(&allActionsAfterUpdate, nil).Once()

		// Read called for TestStep 2, returns ("alert", "deny") for this policy
		client.On("GetMalwarePolicyActions",
			testutils.MockContext,
			appsec.GetMalwarePolicyActionsRequest{ConfigID: 43253, Version: 7, PolicyID: "AAAA_81230"},
		).Return(&allActionsAfterUpdate, nil).Once()

		// Delete, returns ("none", "none") and does NOT generate a Read
		client.On("UpdateMalwarePolicyAction",
			testutils.MockContext,
			appsec.UpdateMalwarePolicyActionRequest{ConfigID: 43253, Version: 7, PolicyID: "AAAA_81230", MalwarePolicyID: 135355, Action: "none", UnscannedAction: "none"},
		).Return(&actionAfterDelete, nil).Once()

		useClient(client, func() {
			resource.Test(t, resource.TestCase{
				IsUnitTest:               true,
				ProtoV6ProviderFactories: testutils.NewProtoV6ProviderFactory(NewSubprovider()),
				Steps: []resource.TestStep{
					{
						Config: testutils.LoadFixtureString(t, "testdata/TestResMalwarePolicyAction/create.tf"),
						Check: resource.ComposeAggregateTestCheckFunc(
							resource.TestCheckResourceAttr("akamai_appsec_malware_policy_action.test", "id", "43253:AAAA_81230:135355"),
							resource.TestCheckResourceAttr("akamai_appsec_malware_policy_action.test", "action", "none"),
							resource.TestCheckResourceAttr("akamai_appsec_malware_policy_action.test", "unscanned_action", "none"),
						),
					},
					{
						Config: testutils.LoadFixtureString(t, "testdata/TestResMalwarePolicyAction/update.tf"),
						Check: resource.ComposeAggregateTestCheckFunc(
							resource.TestCheckResourceAttr("akamai_appsec_malware_policy_action.test", "id", "43253:AAAA_81230:135355"),
							resource.TestCheckResourceAttr("akamai_appsec_malware_policy_action.test", "action", "alert"),
							resource.TestCheckResourceAttr("akamai_appsec_malware_policy_action.test", "unscanned_action", "deny"),
						),
					},
				},
			})
		})

		client.AssertExpectations(t)
	})

}
