package sqs_test

import (
	"fmt"
	"testing"

	"github.com/aws/aws-sdk-go/service/sqs"
	sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
	"github.com/hashicorp/terraform-provider-aws/internal/acctest"
	tfsqs "github.com/hashicorp/terraform-provider-aws/internal/service/sqs"
)

func TestAccSQSQueueRedrivePolicy_basic(t *testing.T) {
	var queueAttributes map[string]string
	resourceName := "aws_sqs_queue_redrive_policy.test"
	queueResourceName := "aws_sqs_queue.test"
	rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:                 func() { acctest.PreCheck(t) },
		ErrorCheck:               acctest.ErrorCheck(t, sqs.EndpointsID),
		ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
		CheckDestroy:             testAccCheckQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccQueueRedrivePolicyConfig_basic(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckQueueExists(queueResourceName, &queueAttributes),
					testAccCheckQueueExists(fmt.Sprintf("%s_ddl", queueResourceName), &queueAttributes),
					resource.TestCheckResourceAttrSet(resourceName, "redrive_policy"),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
			{
				Config:   testAccQueueRedrivePolicyConfig_basic(rName),
				PlanOnly: true,
				Check: resource.ComposeTestCheckFunc(
					resource.TestCheckResourceAttrPair(resourceName, "redrive_policy", queueResourceName, "redrive_policy"),
				),
			},
		},
	})
}

func TestAccSQSQueueRedrivePolicy_disappears(t *testing.T) {
	var queueAttributes map[string]string
	resourceName := "aws_sqs_queue_redrive_policy.test"
	queueResourceName := "aws_sqs_queue.test"
	rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:                 func() { acctest.PreCheck(t) },
		ErrorCheck:               acctest.ErrorCheck(t, sqs.EndpointsID),
		ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
		CheckDestroy:             testAccCheckQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccQueueRedrivePolicyConfig_basic(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckQueueExists(queueResourceName, &queueAttributes),
					testAccCheckQueueExists(fmt.Sprintf("%s_ddl", queueResourceName), &queueAttributes),
					acctest.CheckResourceDisappears(acctest.Provider, tfsqs.ResourceQueueRedrivePolicy(), resourceName),
				),
				ExpectNonEmptyPlan: true,
			},
		},
	})
}

func TestAccSQSQueueRedrivePolicy_Disappears_queue(t *testing.T) {
	var queueAttributes map[string]string
	queueResourceName := "aws_sqs_queue.test"
	rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:                 func() { acctest.PreCheck(t) },
		ErrorCheck:               acctest.ErrorCheck(t, sqs.EndpointsID),
		ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
		CheckDestroy:             testAccCheckQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccQueueRedrivePolicyConfig_basic(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckQueueExists(queueResourceName, &queueAttributes),
					acctest.CheckResourceDisappears(acctest.Provider, tfsqs.ResourceQueue(), queueResourceName),
				),
				ExpectNonEmptyPlan: true,
			},
		},
	})
}

func TestAccSQSQueueRedrivePolicy_update(t *testing.T) {
	var queueAttributes map[string]string
	resourceName := "aws_sqs_queue_redrive_policy.test"
	queueResourceName := "aws_sqs_queue.test"
	rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:                 func() { acctest.PreCheck(t) },
		ErrorCheck:               acctest.ErrorCheck(t, sqs.EndpointsID),
		ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
		CheckDestroy:             testAccCheckQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccQueueRedrivePolicyConfig_basic(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckQueueExists(queueResourceName, &queueAttributes),
					resource.TestCheckResourceAttrSet(resourceName, "redrive_policy"),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
			{
				Config: testAccQueueRedrivePolicyConfig_updated(rName),
				Check: resource.ComposeTestCheckFunc(
					resource.TestCheckResourceAttrSet(resourceName, "redrive_policy"),
				),
			},
		},
	})
}

func testAccQueueRedrivePolicyConfig_basic(rName string) string {
	return fmt.Sprintf(`
resource "aws_sqs_queue" "test" {
  name = %[1]q
}

resource "aws_sqs_queue" "test_ddl" {
  name = "%[1]s_ddl"
  redrive_allow_policy = jsonencode({
    redrivePermission = "byQueue",
    sourceQueueArns   = [aws_sqs_queue.test.arn]
  })
}

resource "aws_sqs_queue_redrive_policy" "test" {
  queue_url = aws_sqs_queue.test.id
  redrive_policy = jsonencode({
    deadLetterTargetArn = aws_sqs_queue.test_ddl.arn
    maxReceiveCount     = 4
  })
}
`, rName)
}

func testAccQueueRedrivePolicyConfig_updated(rName string) string {
	return fmt.Sprintf(`
resource "aws_sqs_queue" "test" {
  name = %[1]q
}

resource "aws_sqs_queue" "test_ddl" {
  name = "%[1]s_ddl"
  redrive_allow_policy = jsonencode({
    redrivePermission = "byQueue",
    sourceQueueArns   = [aws_sqs_queue.test.arn]
  })
}

resource "aws_sqs_queue_redrive_policy" "test" {
  queue_url = aws_sqs_queue.test.id
  redrive_policy = jsonencode({
    deadLetterTargetArn = aws_sqs_queue.test_ddl.arn
    maxReceiveCount     = 2
  })
}
`, rName)
}
