package commontasks

import (
	"context"
	"errors"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/utils/ptr"
	"sigs.k8s.io/controller-runtime/pkg/client"

	agentv2 "github.com/tidbcloud/observability/agent-control-server/api/v2"
	"github.com/tidbcloud/observability/agent-control-server/pkg/config"
	"github.com/tidbcloud/observability/agent-control-server/pkg/consts"
	"github.com/tidbcloud/observability/agent-control-server/pkg/deploy"
	"github.com/tidbcloud/observability/agent-control-server/pkg/o11y"
	"github.com/tidbcloud/observability/agent-control-server/pkg/o11y/api"
	"github.com/tidbcloud/observability/agent-control-server/pkg/o11y/mockapi"
	"github.com/tidbcloud/observability/agent-control-server/pkg/reconciler"
	"github.com/tidbcloud/observability/agent-control-server/pkg/testkits"
	"github.com/tidbcloud/observability/agent-control-server/pkg/util"
)

func makeBaseBackendTask[T client.Object](agent T, mockAPI *mockapi.MockO11yAPI, cfg config.ObservabilityInfra, backendCfg BackendConfig) *BaseBackendTask[T] {
	fakeCli := testkits.BuildClient(agent)
	ctx := &reconciler.Context[T]{
		Agent:  agent,
		Local:  fakeCli,
		Config: &config.RootConfig{O11yConfig: cfg},
	}

	var o11yV2 agentv2.O11yConfigV2
	var bizType string
	switch a := any(agent).(type) {
	case *agentv2.SharedPoolAgent:
		o11yV2 = a.Spec.O11y
		bizType = a.Spec.O11y.BizType
	case *agentv2.TiDBGroupAgent:
		o11yV2 = a.Spec.O11y
		bizType = a.Spec.O11y.BizType
	case *agentv2.KubernetesAgent:
		o11yV2 = a.Spec.O11y
		bizType = a.Spec.O11y.BizType
	}

	task := NewBaseBackendTask(ctx, deploy.ClusterMeta{}, o11yV2, bizType, backendCfg)
	task.Inner.O11yAPI = mockAPI
	return task
}

func TestBaseBackendTask_Create(t *testing.T) {
	tests := []struct {
		name      string
		agent     *agentv2.SharedPoolAgent
		config    config.ObservabilityInfra
		mockSetup func(*mockapi.MockO11yAPI)
		verify    func(t *testing.T, agent *agentv2.SharedPoolAgent)
		result    deploy.Result
	}{
		{
			name: "create success with prod env",
			agent: &agentv2.SharedPoolAgent{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-agent",
					Namespace: "default",
				},
				Spec: agentv2.SharedPoolAgentSpec{
					O11y: agentv2.O11yConfigV2{
						TenantID:      "tenant-1",
						ApplicationID: "app-1",
						BizType:       "tidbcloud",
					},
				},
			},
			config: config.ObservabilityInfra{
				Env:         "prod",
				Vendor:      "aws",
				LogicRegion: "us-east-1",
			},
			mockSetup: func(m *mockapi.MockO11yAPI) {
				m.On("Create", mock.MatchedBy(func(p o11y.Params) bool {
					return p.Vendor() == "aws" && p.Region() == "us-east-1"
				})).Return(&api.ObservabilityCluster{
					ClusterBaseInfo: api.ClusterBaseInfo{ID: "cluster-1"},
					MonitorInfo: api.MonitorInfo{
						DnsName:     ptr.To("test.dns.com"),
						VMAccountID: "vm-acc-1",
						VMProjectID: "vm-proj-1",
					},
					StorageInfo: []api.StorageInfo{{Category: api.LOGS, BucketName: "log-bucket"}},
					AgentInfo:   api.AgentInfo{RoleARN: "role-arn"},
					Status:      api.ACTIVE,
				}, nil)
			},
			verify: func(t *testing.T, agent *agentv2.SharedPoolAgent) {
				assert.Equal(t, "cluster-1", util.GetAnnotation(agent, consts.AnnotationKeyO11yID))
				assert.Equal(t, "test.dns.com", util.GetAnnotation(agent, consts.AnnotationKeyO11yServiceDNSName))
			},
			result: deploy.Done(),
		},
		{
			name: "create success with dev env",
			agent: &agentv2.SharedPoolAgent{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-agent",
					Namespace: "default",
				},
				Spec: agentv2.SharedPoolAgentSpec{
					O11y: agentv2.O11yConfigV2{
						TenantID:      "tenant-1",
						ApplicationID: "app-1",
						BizType:       "tidbcloud",
					},
				},
			},
			config: config.ObservabilityInfra{
				Env:         "dev",
				Vendor:      "aws",
				LogicRegion: "us-east-1",
			},
			mockSetup: func(m *mockapi.MockO11yAPI) {
				m.On("Create", mock.Anything).Return(&api.ObservabilityCluster{
					ClusterBaseInfo: api.ClusterBaseInfo{ID: "cluster-1"},
					MonitorInfo: api.MonitorInfo{
						DnsName:     ptr.To("test.dns.com"),
						VMAccountID: "vm-acc-1",
						VMProjectID: "vm-proj-1",
					},
					StorageInfo: []api.StorageInfo{{Category: api.LOGS, BucketName: "log-bucket"}},
					Status:      api.ACTIVE,
				}, nil)
			},
			verify: func(t *testing.T, agent *agentv2.SharedPoolAgent) {
				assert.Equal(t, "cluster-1", util.GetAnnotation(agent, consts.AnnotationKeyO11yID))
			},
			result: deploy.Done(),
		},
		{
			name: "create with BYOC mode",
			agent: &agentv2.SharedPoolAgent{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-agent",
					Namespace: "default",
				},
				Spec: agentv2.SharedPoolAgentSpec{
					O11y: agentv2.O11yConfigV2{
						TenantID:      "tenant-1",
						ApplicationID: "app-1",
						BizType:       "tidbcloud",
					},
				},
			},
			config: config.ObservabilityInfra{
				Env:         "prod",
				Vendor:      "aws",
				LogicRegion: "us-east-1",
				IsByoc:      true,
			},
			mockSetup: func(m *mockapi.MockO11yAPI) {
				m.On("Create", mock.Anything).Return(&api.ObservabilityCluster{
					ClusterBaseInfo: api.ClusterBaseInfo{ID: "cluster-1"},
					MonitorInfo: api.MonitorInfo{
						DnsName:     ptr.To("test.dns.com"),
						VMAccountID: "vm-acc-1",
						VMProjectID: "vm-proj-1",
					},
					StorageInfo: []api.StorageInfo{{Category: api.LOGS, BucketName: "log-bucket"}},
					Status:      api.ACTIVE,
				}, nil)
			},
			verify: func(t *testing.T, agent *agentv2.SharedPoolAgent) {
				assert.Equal(t, "cluster-1", util.GetAnnotation(agent, consts.AnnotationKeyO11yID))
			},
			result: deploy.Done(),
		},
		{
			name: "create with projectID from label",
			agent: &agentv2.SharedPoolAgent{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-agent",
					Namespace: "default",
					Labels: map[string]string{
						consts.LabelKeyO11yProjectID: "project-123",
					},
				},
				Spec: agentv2.SharedPoolAgentSpec{
					O11y: agentv2.O11yConfigV2{
						TenantID:      "tenant-1",
						ApplicationID: "app-1",
						BizType:       "tidbcloud",
					},
				},
			},
			config: config.ObservabilityInfra{
				Env:         "prod",
				Vendor:      "aws",
				LogicRegion: "us-east-1",
			},
			mockSetup: func(m *mockapi.MockO11yAPI) {
				m.On("Create", mock.Anything).Return(&api.ObservabilityCluster{
					ClusterBaseInfo: api.ClusterBaseInfo{ID: "cluster-1"},
					MonitorInfo: api.MonitorInfo{
						DnsName:     ptr.To("test.dns.com"),
						VMAccountID: "vm-acc-1",
						VMProjectID: "vm-proj-1",
					},
					StorageInfo: []api.StorageInfo{{Category: api.LOGS, BucketName: "log-bucket"}},
					Status:      api.ACTIVE,
				}, nil)
			},
			verify: func(t *testing.T, agent *agentv2.SharedPoolAgent) {
				assert.Equal(t, "cluster-1", util.GetAnnotation(agent, consts.AnnotationKeyO11yID))
			},
			result: deploy.Done(),
		},
		{
			name: "peering NLB: uses NLB address instead of backend DNS without endpoint service name",
			agent: &agentv2.SharedPoolAgent{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-agent",
					Namespace: "default",
				},
				Spec: agentv2.SharedPoolAgentSpec{
					O11y: agentv2.O11yConfigV2{
						TenantID:      "tenant-1",
						ApplicationID: "app-1",
						BizType:       "tidbcloud",
					},
				},
			},
			config: config.ObservabilityInfra{
				Env:         "prod",
				Vendor:      "aws",
				LogicRegion: "us-east-1",
				PeeringNLB:  "internal-nlb.us-east-1.elb.amazonaws.com",
			},
			mockSetup: func(m *mockapi.MockO11yAPI) {
				m.On("Create", mock.MatchedBy(func(p o11y.Params) bool {
					return p.Vendor() == "aws" && p.Region() == "us-east-1"
				})).Return(&api.ObservabilityCluster{
					ClusterBaseInfo: api.ClusterBaseInfo{ID: "cluster-peering"},
					MonitorInfo: api.MonitorInfo{
						DnsName:     ptr.To("backend.us-east-1.elb.amazonaws.com"),
						VMAccountID: "vm-acc-peering",
						VMProjectID: "vm-proj-peering",
					},
					StorageInfo: []api.StorageInfo{{Category: api.LOGS, BucketName: "log-bucket-peering"}},
					AgentInfo:   api.AgentInfo{RoleARN: "role-arn-peering"},
					Status:      api.ACTIVE,
				}, nil)
			},
			verify: func(t *testing.T, agent *agentv2.SharedPoolAgent) {
				assert.Equal(t, "cluster-peering", util.GetAnnotation(agent, consts.AnnotationKeyO11yID))
				assert.Equal(t, "internal-nlb.us-east-1.elb.amazonaws.com", util.GetAnnotation(agent, consts.AnnotationKeyO11yServiceDNSName))
				assert.Empty(t, util.GetAnnotation(agent, consts.AnnotationKeyO11yEndpointServiceName))
			},
			result: deploy.Done(),
		},
		{
			name: "azure create succeeds without backend dns because private endpoint ip task fills it later",
			agent: &agentv2.SharedPoolAgent{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-agent",
					Namespace: "default",
				},
				Spec: agentv2.SharedPoolAgentSpec{
					O11y: agentv2.O11yConfigV2{
						TenantID:      "tenant-1",
						ApplicationID: "app-1",
						BizType:       "dev-tidbcloud-nextgen",
					},
				},
			},
			config: config.ObservabilityInfra{
				Env:         "dev",
				Vendor:      "azure",
				LogicRegion: "westus3",
			},
			mockSetup: func(m *mockapi.MockO11yAPI) {
				m.On("Create", mock.MatchedBy(func(p o11y.Params) bool {
					return p.Vendor() == "azure" && p.Region() == "westus3" && p.SkipEndpointServiceName()
				})).Return(&api.ObservabilityCluster{
					ClusterBaseInfo: api.ClusterBaseInfo{ID: "cluster-azure"},
					MonitorInfo: api.MonitorInfo{
						DnsName:     nil,
						VMAccountID: "vm-acc-azure",
						VMProjectID: "vm-proj-azure",
					},
					StorageInfo: []api.StorageInfo{{Category: api.LOGS, BucketName: "log-bucket-azure"}},
					Status:      api.ACTIVE,
				}, nil)
			},
			verify: func(t *testing.T, agent *agentv2.SharedPoolAgent) {
				assert.Equal(t, "cluster-azure", util.GetAnnotation(agent, consts.AnnotationKeyO11yID))
				assert.Empty(t, util.GetAnnotation(agent, consts.AnnotationKeyO11yServiceDNSName))
				assert.Empty(t, util.GetAnnotation(agent, consts.AnnotationKeyO11yServiceOriginDNSName))
			},
			result: deploy.Done(),
		},
		{
			name: "create failed",
			agent: &agentv2.SharedPoolAgent{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-agent",
					Namespace: "default",
				},
				Spec: agentv2.SharedPoolAgentSpec{
					O11y: agentv2.O11yConfigV2{
						TenantID:      "tenant-1",
						ApplicationID: "app-1",
						BizType:       "tidbcloud",
					},
				},
			},
			config: config.ObservabilityInfra{
				Env:         "prod",
				Vendor:      "aws",
				LogicRegion: "us-east-1",
			},
			mockSetup: func(m *mockapi.MockO11yAPI) {
				m.On("Create", mock.Anything).Return(nil, errors.New("create failed"))
			},
			result: deploy.Panic(errors.New("create failed")),
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			mockAPI := new(mockapi.MockO11yAPI)
			if tt.mockSetup != nil {
				tt.mockSetup(mockAPI)
			}

			task := makeBaseBackendTask[*agentv2.SharedPoolAgent](tt.agent, mockAPI, tt.config, DataPlaneBackendConfig())
			res := task.Create(context.Background())
			assert.Equal(t, "Backend", task.Name())
			assert.Equal(t, deploy.ClusterMeta{}, task.ClusterMeta())
			assert.Equal(t, tt.result.IsDoneState(), res.Error() == nil)

			if tt.verify != nil {
				tt.verify(t, tt.agent)
			}
			mockAPI.AssertExpectations(t)
		})
	}
}

func TestBaseBackendTask_Delete(t *testing.T) {
	tests := []struct {
		name      string
		agent     *agentv2.TiDBGroupAgent
		config    config.ObservabilityInfra
		mockSetup func(*mockapi.MockO11yAPI)
		result    deploy.Result
	}{
		{
			name: "delete success",
			agent: &agentv2.TiDBGroupAgent{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-agent",
					Namespace: "default",
					Annotations: map[string]string{
						consts.AnnotationKeyO11yID: "cluster-1",
					},
				},
				Spec: agentv2.TiDBGroupAgentSpec{
					O11y: agentv2.O11yConfigV2{
						TenantID:      "tenant-1",
						ApplicationID: "app-1",
						BizType:       "tidbcloud",
					},
				},
			},
			config: config.ObservabilityInfra{
				Env:         "prod",
				Vendor:      "aws",
				LogicRegion: "us-east-1",
			},
			mockSetup: func(m *mockapi.MockO11yAPI) {
				m.On("Delete", mock.Anything).Return(&api.ObservabilityCluster{
					ClusterBaseInfo: api.ClusterBaseInfo{ID: "cluster-1"},
					Status:          api.DELETING,
				}, nil)
			},
			result: deploy.Done(),
		},
		{
			name: "delete failed",
			agent: &agentv2.TiDBGroupAgent{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-agent",
					Namespace: "default",
					Annotations: map[string]string{
						consts.AnnotationKeyO11yID: "cluster-1",
					},
				},
				Spec: agentv2.TiDBGroupAgentSpec{
					O11y: agentv2.O11yConfigV2{
						TenantID:      "tenant-1",
						ApplicationID: "app-1",
						BizType:       "tidbcloud",
					},
				},
			},
			config: config.ObservabilityInfra{
				Env:         "prod",
				Vendor:      "aws",
				LogicRegion: "us-east-1",
			},
			mockSetup: func(m *mockapi.MockO11yAPI) {
				m.On("Delete", mock.Anything).Return(nil, errors.New("delete failed"))
			},
			result: deploy.Panic(errors.New("delete failed")),
		},
		{
			name: "delete skipped with preserve label",
			agent: &agentv2.TiDBGroupAgent{
				ObjectMeta: metav1.ObjectMeta{
					Name:      "test-agent",
					Namespace: "default",
					Labels: map[string]string{
						consts.LabelKeyPreserveO11yCluster: "true",
					},
					Annotations: map[string]string{
						consts.AnnotationKeyO11yID: "cluster-1",
					},
				},
				Spec: agentv2.TiDBGroupAgentSpec{
					O11y: agentv2.O11yConfigV2{
						TenantID:      "tenant-1",
						ApplicationID: "app-1",
						BizType:       "tidbcloud",
					},
				},
			},
			config: config.ObservabilityInfra{
				Env:         "prod",
				Vendor:      "aws",
				LogicRegion: "us-east-1",
			},
			mockSetup: func(m *mockapi.MockO11yAPI) {
				// No API call expected
			},
			result: deploy.Done(),
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			mockAPI := new(mockapi.MockO11yAPI)
			if tt.mockSetup != nil {
				tt.mockSetup(mockAPI)
			}

			task := makeBaseBackendTask[*agentv2.TiDBGroupAgent](tt.agent, mockAPI, tt.config, DataPlaneBackendConfig())
			res := task.Delete(context.Background())
			assert.Equal(t, tt.result.IsDoneState(), res.Error() == nil)
			mockAPI.AssertExpectations(t)
		})
	}
}

func TestBaseBackendTask_Create_ByocPeeringPassesRouteInputs(t *testing.T) {
	agent := &agentv2.KubernetesAgent{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-agent",
			Namespace: "default",
			Annotations: map[string]string{
				consts.AnnotationKeyBYOCVPCPeeringID:     "pcx-123",
				consts.AnnotationKeyBYOCDataplaneVPCCIDR: "10.30.0.0/16",
			},
		},
		Spec: agentv2.KubernetesAgentSpec{
			O11y: agentv2.O11yConfigV2{
				TenantID:      "tenant-1",
				ApplicationID: "app-1",
				BizType:       "dev-tidbcloud-nextgen",
			},
			EKS: &agentv2.EKSConfig{AccountID: "123456789012"},
		},
	}

	mockAPI := new(mockapi.MockO11yAPI)
	mockAPI.On("Create", mock.MatchedBy(func(p o11y.Params) bool {
		peeringID, cidr := p.BYOCVPCPeering()
		return p.Vendor() == "aws" &&
			p.Region() == "us-east-1" &&
			p.AgentAccessAccount() == "123456789012" &&
			peeringID == "pcx-123" &&
			cidr == "10.30.0.0/16"
	})).Return(&api.ObservabilityCluster{
		ClusterBaseInfo: api.ClusterBaseInfo{ID: "cluster-byoc"},
		MonitorInfo: api.MonitorInfo{
			VMAccountID: "vm-acc-byoc",
			VMProjectID: "vm-proj-byoc",
		},
		StorageInfo: []api.StorageInfo{{Category: api.LOGS, BucketName: "log-bucket-byoc"}},
		AgentInfo:   api.AgentInfo{RoleARN: "role-arn-byoc"},
		Status:      api.ACTIVE,
	}, nil)

	task := makeBaseBackendTask[*agentv2.KubernetesAgent](agent, mockAPI, config.ObservabilityInfra{
		Env:         "prod",
		Vendor:      "aws",
		LogicRegion: "us-east-1",
		IsByoc:      true,
		PeeringNLB:  "internal-o11y.example.com",
	}, func() BackendConfig {
		cfg := ControlPlaneBackendConfig(agent.Spec.EKS.AccountID)
		cfg.RequirePeering = true
		return cfg
	}())

	res := task.Create(context.Background())
	assert.True(t, res.IsDoneState())
	assert.Equal(t, "internal-o11y.example.com", util.GetAnnotation(agent, consts.AnnotationKeyO11yServiceDNSName))
	mockAPI.AssertExpectations(t)
}

func TestBaseBackendTask_Create_ByocPeeringRequiresAnnotations(t *testing.T) {
	agent := &agentv2.KubernetesAgent{
		ObjectMeta: metav1.ObjectMeta{Name: "test-agent", Namespace: "default"},
		Spec: agentv2.KubernetesAgentSpec{
			O11y: agentv2.O11yConfigV2{TenantID: "tenant-1", ApplicationID: "app-1", BizType: "dev-tidbcloud-nextgen"},
			EKS:  &agentv2.EKSConfig{AccountID: "123456789012"},
		},
	}

	mockAPI := new(mockapi.MockO11yAPI)
	task := makeBaseBackendTask[*agentv2.KubernetesAgent](agent, mockAPI, config.ObservabilityInfra{
		Env:         "prod",
		Vendor:      "aws",
		LogicRegion: "us-east-1",
		IsByoc:      true,
		PeeringNLB:  "internal-o11y.example.com",
	}, func() BackendConfig {
		cfg := ControlPlaneBackendConfig(agent.Spec.EKS.AccountID)
		cfg.RequirePeering = true
		return cfg
	}())

	res := task.Create(context.Background())
	assert.False(t, res.IsDoneState())
	mockAPI.AssertNotCalled(t, "Create", mock.Anything)
}

func TestBaseBackendTask_Create_ByocPeeringNotRequiredForSharedPoolAgent(t *testing.T) {
	agent := &agentv2.SharedPoolAgent{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-agent",
			Namespace: "default",
		},
		Spec: agentv2.SharedPoolAgentSpec{
			O11y: agentv2.O11yConfigV2{
				TenantID:      "tenant-1",
				ApplicationID: "app-1",
				BizType:       "dev-tidbcloud-nextgen",
			},
		},
	}

	mockAPI := new(mockapi.MockO11yAPI)
	mockAPI.On("Create", mock.MatchedBy(func(p o11y.Params) bool {
		peeringID, cidr := p.BYOCVPCPeering()
		return p.Vendor() == "aws" &&
			p.Region() == "us-east-1" &&
			peeringID == "" &&
			cidr == ""
	})).Return(&api.ObservabilityCluster{
		ClusterBaseInfo: api.ClusterBaseInfo{ID: "cluster-sharedpool"},
		MonitorInfo: api.MonitorInfo{
			VMAccountID: "vm-acc-sharedpool",
			VMProjectID: "vm-proj-sharedpool",
		},
		StorageInfo: []api.StorageInfo{{Category: api.LOGS, BucketName: "log-bucket-sharedpool"}},
		AgentInfo:   api.AgentInfo{RoleARN: "role-arn-sharedpool"},
		Status:      api.ACTIVE,
	}, nil)

	task := makeBaseBackendTask[*agentv2.SharedPoolAgent](agent, mockAPI, config.ObservabilityInfra{
		Env:         "prod",
		Vendor:      "aws",
		LogicRegion: "us-east-1",
		IsByoc:      true,
		PeeringNLB:  "internal-o11y.example.com",
	}, DataPlaneBackendConfig())

	res := task.Create(context.Background())
	assert.True(t, res.IsDoneState())
	mockAPI.AssertExpectations(t)
}

func TestBaseBackendTask_Create_AzureK8sAgentUsesSubscriptionIDAsAccount(t *testing.T) {
	agent := &agentv2.KubernetesAgent{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test-agent",
			Namespace: "default",
		},
		Spec: agentv2.KubernetesAgentSpec{
			O11y: agentv2.O11yConfigV2{
				TenantID:      "tenant-1",
				ApplicationID: "app-1",
				BizType:       "dev-tidbcloud-nextgen",
			},
			AKS: &agentv2.AKSConfig{
				SubscriptionID: "sub-123",
			},
		},
	}

	mockAPI := new(mockapi.MockO11yAPI)
	mockAPI.On("Create", mock.MatchedBy(func(p o11y.Params) bool {
		return p.Vendor() == "azure" &&
			p.Region() == "westus3" &&
			p.AgentAccessAccount() == "sub-123" &&
			p.SkipEndpointServiceName()
	})).Return(&api.ObservabilityCluster{
		ClusterBaseInfo: api.ClusterBaseInfo{ID: "cluster-azure-k8s"},
		MonitorInfo: api.MonitorInfo{
			EndpointServiceName: "azure-pls-id",
			DnsName:             nil,
			VMAccountID:         "vm-acc-azure",
			VMProjectID:         "vm-proj-azure",
		},
		StorageInfo: []api.StorageInfo{{Category: api.LOGS, BucketName: "log-bucket-azure"}},
		Status:      api.ACTIVE,
	}, nil)

	backendCfg := ControlPlaneBackendConfig(agent.Spec.AKS.SubscriptionID)
	backendCfg.SkipBackendDNSName = true

	task := makeBaseBackendTask[*agentv2.KubernetesAgent](agent, mockAPI, config.ObservabilityInfra{
		Env:         "dev",
		Vendor:      "azure",
		LogicRegion: "westus3",
	}, backendCfg)

	res := task.Create(context.Background())
	assert.True(t, res.IsDoneState())
	assert.Equal(t, "cluster-azure-k8s", util.GetAnnotation(agent, consts.AnnotationKeyO11yID))
	assert.Equal(t, "azure-pls-id", util.GetAnnotation(agent, consts.AnnotationKeyO11yEndpointServiceName))
	assert.Empty(t, util.GetAnnotation(agent, consts.AnnotationKeyO11yServiceDNSName))
	mockAPI.AssertExpectations(t)
}
