package inspector2_test

import (
	"context"
	"errors"
	"fmt"
	"testing"

	"github.com/aws/aws-sdk-go-v2/service/inspector2"
	"github.com/aws/aws-sdk-go-v2/service/inspector2/types"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
	"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
	"github.com/hashicorp/terraform-provider-aws/internal/acctest"
	"github.com/hashicorp/terraform-provider-aws/internal/conns"
	"github.com/hashicorp/terraform-provider-aws/internal/create"
	"github.com/hashicorp/terraform-provider-aws/internal/errs"
	tfinspector2 "github.com/hashicorp/terraform-provider-aws/internal/service/inspector2"
	"github.com/hashicorp/terraform-provider-aws/names"
)

func TestAccInspector2DelegatedAdminAccount_serial(t *testing.T) {
	testCases := map[string]func(t *testing.T){
		"basic":      testAccDelegatedAdminAccount_basic,
		"disappears": testAccDelegatedAdminAccount_disappears,
	}

	for name, tc := range testCases {
		tc := tc
		t.Run(name, func(t *testing.T) {
			tc(t)
		})
	}
}

func testAccDelegatedAdminAccount_basic(t *testing.T) {
	resourceName := "aws_inspector2_delegated_admin_account.test"

	resource.Test(t, resource.TestCase{
		PreCheck: func() {
			acctest.PreCheck(t)
			acctest.PreCheckPartitionHasService(names.Inspector2EndpointID, t)
			testAccPreCheck(t)
			acctest.PreCheckOrganizationManagementAccount(t)
		},
		ErrorCheck:               acctest.ErrorCheck(t, names.Inspector2EndpointID),
		ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
		CheckDestroy:             testAccCheckDelegatedAdminAccountDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccDelegatedAdminAccountConfig_basic(),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckDelegatedAdminAccountExists(resourceName),
					resource.TestCheckResourceAttrPair(resourceName, "account_id", "data.aws_caller_identity.current", "account_id"),
					resource.TestCheckResourceAttrSet(resourceName, "relationship_status"),
				),
			},
			{
				ResourceName: resourceName,
				ImportState:  true,
				ImportStateCheck: func(s []*terraform.InstanceState) error {
					idx := -1
					for i, rs := range s {
						if v, ok := rs.Attributes["relationship_status"]; ok && v != "" {
							idx = i
						}
					}

					if idx == -1 {
						return fmt.Errorf("expected aws_inspector2_delegated_admin_account to be in state, not found")
					}

					rs := s[idx]

					if rs.Attributes["relationship_status"] != string(types.RelationshipStatusEnabled) {
						return fmt.Errorf("expected relationship_status attribute to be set and be %s, received: %s", string(types.RelationshipStatusEnabled), rs.Attributes["relationship_status"])
					}

					return nil
				},
			},
		},
	})
}

func testAccDelegatedAdminAccount_disappears(t *testing.T) {
	resourceName := "aws_inspector2_delegated_admin_account.test"

	resource.Test(t, resource.TestCase{
		PreCheck: func() {
			acctest.PreCheck(t)
			acctest.PreCheckPartitionHasService(names.Inspector2EndpointID, t)
			testAccPreCheck(t)
			acctest.PreCheckOrganizationManagementAccount(t)
		},
		ErrorCheck:               acctest.ErrorCheck(t, names.Inspector2EndpointID),
		ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
		CheckDestroy:             testAccCheckDelegatedAdminAccountDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccDelegatedAdminAccountConfig_basic(),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckDelegatedAdminAccountExists(resourceName),
					acctest.CheckResourceDisappears(acctest.Provider, tfinspector2.ResourceDelegatedAdminAccount(), resourceName),
				),
				ExpectNonEmptyPlan: true,
			},
		},
	})
}

func testAccCheckDelegatedAdminAccountDestroy(s *terraform.State) error {
	conn := acctest.Provider.Meta().(*conns.AWSClient).Inspector2Client()
	ctx := context.Background()

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_inspector2_delegated_admin_account" {
			continue
		}

		st, _, err := tfinspector2.FindDelegatedAdminAccountStatusID(ctx, conn, rs.Primary.ID)

		if st == "" && errs.Contains(err, "admin account not found") {
			return nil
		}

		if err != nil {
			return err
		}

		return create.Error(names.Inspector2, create.ErrActionCheckingDestroyed, tfinspector2.ResNameDelegatedAdminAccount, rs.Primary.ID, errors.New("not destroyed"))
	}

	return nil
}

func testAccCheckDelegatedAdminAccountExists(name string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[name]
		if !ok {
			return create.Error(names.Inspector2, create.ErrActionCheckingExistence, tfinspector2.ResNameDelegatedAdminAccount, name, errors.New("not found"))
		}

		if rs.Primary.ID == "" {
			return create.Error(names.Inspector2, create.ErrActionCheckingExistence, tfinspector2.ResNameDelegatedAdminAccount, name, errors.New("not set"))
		}

		conn := acctest.Provider.Meta().(*conns.AWSClient).Inspector2Client()
		ctx := context.Background()
		_, _, err := tfinspector2.FindDelegatedAdminAccountStatusID(ctx, conn, rs.Primary.ID)

		if err != nil {
			return create.Error(names.Inspector2, create.ErrActionCheckingExistence, tfinspector2.ResNameDelegatedAdminAccount, rs.Primary.ID, err)
		}

		return nil
	}
}

func testAccPreCheck(t *testing.T) {
	conn := acctest.Provider.Meta().(*conns.AWSClient).Inspector2Client()
	ctx := context.Background()

	_, err := conn.ListDelegatedAdminAccounts(ctx, &inspector2.ListDelegatedAdminAccountsInput{})

	if acctest.PreCheckSkipError(err) {
		t.Skipf("skipping acceptance testing: %s", err)
	}

	if err != nil {
		t.Fatalf("unexpected PreCheck error: %s", err)
	}
}

func testAccDelegatedAdminAccountConfig_basic() string {
	return `
data "aws_caller_identity" "current" {}

resource "aws_inspector2_delegated_admin_account" "test" {
  account_id = data.aws_caller_identity.current.account_id
}
`
}
