/*
Copyright 2024.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package integrationtask

import (
	"context"
	"testing"

	"github.com/go-logr/logr/testr"
	"github.com/stretchr/testify/assert"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	o11yv1 "github.com/tidbcloud/observability/api/regionalserver/resourceworker/v1"
	"github.com/tidbcloud/observability/pkg/common/constants"
	"github.com/tidbcloud/observability/pkg/common/structs"
	"github.com/tidbcloud/observability/pkg/resourceworker/controllers/integrationtask/v2/task"
)

func TestPopulateDestinationInfo_TenantScope_AliCloud(t *testing.T) {
	tt := &o11yv1.IntegrationTask{
		ObjectMeta: metav1.ObjectMeta{
			Namespace: "prod-devtier-tenant123",
			Labels: map[string]string{
				constants.LabelTenantID: "tenant123",
				constants.LabelAlias:    "o11y-ext-tenant123",
			},
		},
		Spec: o11yv1.IntegrationTaskSpec{
			Scope:                 o11yv1.ScopeTenant,
			TaskType:              o11yv1.SqlStmtTask,
			DestinationBucketName: "o11y-prod-external-ap-southeast-5",
		},
	}
	c := &task.Context{
		Task:   tt,
		Logger: testr.New(t),
		ServiceInfo: structs.ServiceInfo{
			Vendor:      "alicloud",
			Region:      "ap-southeast-5",
			AccountInfo: structs.AccountInfoConfig{Account: "123456789012"},
		},
	}

	err := populateDestinationInfo(context.Background(), c)
	assert.NoError(t, err)
	// Tenant path aggregates all clusters; no project/cluster segments.
	assert.Equal(t, "oss://o11y-prod-external-ap-southeast-5/log_data/data/tenant123",
		tt.Status.DestinationInfo.AccessPoint)
	assert.Equal(t, "acs:ram::123456789012:role/o11y-ext-tenant123",
		tt.Status.DestinationInfo.AssumeRoleARN)
}

func TestPopulateDestinationInfo_ClusterScope_AliCloudWithoutProject(t *testing.T) {
	tt := &o11yv1.IntegrationTask{
		ObjectMeta: metav1.ObjectMeta{
			Namespace: "prod-tidbcloud-nextgen-tenant123",
			Labels: map[string]string{
				constants.LabelTenantID:  "tenant123",
				constants.LabelProjectID: "",
				constants.LabelAlias:     "o11y-ext-tenant123",
			},
		},
		Spec: o11yv1.IntegrationTaskSpec{
			Scope:                 o11yv1.ScopeCluster,
			TaskType:              o11yv1.SlowQueryTask,
			ClusterID:             "cluster789",
			DestinationBucketName: "o11y-prod-external-ap-southeast-1",
		},
	}
	c := &task.Context{
		Task:   tt,
		Logger: testr.New(t),
		ServiceInfo: structs.ServiceInfo{
			Vendor:      "alicloud",
			Region:      "ap-southeast-1",
			AccountInfo: structs.AccountInfoConfig{Account: "123456789012"},
		},
	}

	err := populateDestinationInfo(context.Background(), c)
	assert.NoError(t, err)
	assert.Equal(t, "oss://o11y-prod-external-ap-southeast-1/log_data/data/tenant123/cluster789",
		tt.Status.DestinationInfo.AccessPoint)
	assert.Equal(t, "acs:ram::123456789012:role/o11y-ext-tenant123",
		tt.Status.DestinationInfo.AssumeRoleARN)
}
