package gke

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/tidbcloud/infra-provider/pkg/cloud/gcp"
	"github.com/tidbcloud/infra-provider/regional-server/api/v1alpha1"
	"go.uber.org/mock/gomock"
)

func TestGoogleApiPsc_Create(t *testing.T) {
	ctl := gomock.NewController(t)
	gcpcli := gcp.NewMockClient(ctl)
	gcptask := NewGoogleApiPsc(fakeTaskContext(t), gcpcli)
	t.Run("not legacy", func(t *testing.T) {
		gcptask.VPC.Labels = map[string]string{}
		result := gcptask.Create(context.Background())
		assert.True(t, result.IsDoneState())
	})

	t.Run("migrated resource and apipsc is exist", func(t *testing.T) {
		gcptask.VPC.Labels = map[string]string{
			v1alpha1.IsLegacyKey: "true",
		}
		gcpcli.EXPECT().IsGoogleApiPscExists(gomock.Any(), gomock.Any(), gomock.Any()).Return(true, nil)
		result := gcptask.Create(context.Background())
		assert.True(t, result.IsDoneState())
	})
	t.Run("migrated resource and apipsc is not exist", func(t *testing.T) {
		gcptask.VPC.Labels = map[string]string{
			v1alpha1.IsLegacyKey: "true",
		}
		gcpcli.EXPECT().IsGoogleApiPscExists(gomock.Any(), gomock.Any(), gomock.Any()).Return(false, nil)
		gcpcli.EXPECT().DeleteGoogleApiPscDnsZone(gomock.Any(), gomock.Any()).Return(nil)
		result := gcptask.Create(context.Background())
		assert.True(t, result.IsDoneState())
	})
}

func TestGoogleApiPsc_Delete(t *testing.T) {
	ctl := gomock.NewController(t)
	gcpcli := gcp.NewMockClient(ctl)
	gcptask := NewGoogleApiPsc(fakeTaskContext(t), gcpcli)
	t.Run("not legacy", func(t *testing.T) {
		result := gcptask.Delete(context.Background())
		assert.True(t, result.IsDoneState())
	})

	t.Run("migrated resource", func(t *testing.T) {
		gcptask.VPC.Labels = map[string]string{
			v1alpha1.IsLegacyKey: "true",
		}
		gcpcli.EXPECT().DeleteAllGoogleApiPsc(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
		result := gcptask.Delete(context.Background())
		assert.True(t, result.IsDoneState())
	})
}
