package action

import (
	"context"
	"testing"

	"github.com/stretchr/testify/require"
	apiv1 "github.com/tidbcloud/cluster-service-ng/api/cluster-service-ng/v1beta"
	"github.com/tidbcloud/cluster-service-ng/domain/entity"
)

func TestSetTiDBSlowLogTemplateArgsDeletingAllowsMissingStorage(t *testing.T) {
	templArgs := map[string]string{}
	comp := &entity.Component{
		Id:            "tidb-group-1",
		ComponentType: apiv1.ComponentType_TIDB,
		Phase:         apiv1.Component_DELETING,
		NodeQuota:     &apiv1.NodeQuota{NodeCount: 0},
	}
	sharedPool := &entity.SharedPool{
		SharedPool: apiv1.SharedPool{
			Id: "shared-pool-1",
		},
	}

	err := setTiDBSlowLogTemplateArgs(context.Background(), comp, sharedPool, templArgs, true)

	require.NoError(t, err)
	require.Equal(t, "delete-placeholder", templArgs["SlowLogStorageClass"])
	require.Equal(t, "1", templArgs["SlowLogStorageSize"])
	require.Equal(t, entity.AuditAndSlowLogPath, templArgs["SlowLogStoragePath"])
	require.Equal(t, "delete-placeholder", templArgs["SlowLogVolumeAttributesClass"])
}

func TestSetTiDBSlowLogTemplateArgsCreateRequiresStorage(t *testing.T) {
	templArgs := map[string]string{}
	comp := &entity.Component{
		Id:            "tidb-group-1",
		ComponentType: apiv1.ComponentType_TIDB,
		Phase:         apiv1.Component_CREATING,
	}
	sharedPool := &entity.SharedPool{
		SharedPool: apiv1.SharedPool{
			Id: "shared-pool-1",
		},
	}

	err := setTiDBSlowLogTemplateArgs(context.Background(), comp, sharedPool, templArgs, false)

	require.ErrorContains(t, err, "slow log storage class is not set")
}
