package resourcepool

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
	"go.uber.org/mock/gomock"

	globalcommonv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/common/v1"
	globalngv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/nextgen/v1"
	"github.com/tidbcloud/tidb-management-service/nextgen-global-service/domain/entity"
	reporesourcepool "github.com/tidbcloud/tidb-management-service/nextgen-global-service/domain/repository/resourcepool"
	mockresourcepool "github.com/tidbcloud/tidb-management-service/nextgen-global-service/domain/repository/resourcepool/mock_resourcepool"
	"github.com/tidbcloud/tidb-management-service/utilities/bizerr"
)

func TestListOwnerResourcePools_ServicePlanOptional_WhenUnspecified(t *testing.T) {
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()

	strategyRepo := mockresourcepool.NewMockStrategyRepo(ctrl)
	repo := mockresourcepool.NewMockRepo(ctrl)

	s := &serviceImpl{
		resourcePoolRepo:         repo,
		resourcePoolStrategyRepo: strategyRepo,
	}

	repo.EXPECT().Query(gomock.Any(), gomock.Any()).DoAndReturn(
		func(_ context.Context, filter *reporesourcepool.Filter) ([]*entity.ResourcePool, int64, bizerr.BizError) {
			assert.Empty(t, filter.ServicePlans)
			return []*entity.ResourcePool{}, 0, nil
		},
	)
	strategyRepo.EXPECT().Query(gomock.Any(), gomock.Any()).DoAndReturn(
		func(_ context.Context, filter *reporesourcepool.StrategyFilter) ([]*entity.ResourcePoolStrategy, bizerr.BizError) {
			assert.Empty(t, filter.ServicePlans)
			return []*entity.ResourcePoolStrategy{}, nil
		},
	)

	_, _, err := s.ListOwnerResourcePools(context.Background(), &globalngv1.ListOwnerResourcePoolsRequest{
		OwnerType: globalngv1.ResourcePool_ORGANIZATION,
		OwnerId:   100,
	})

	assert.NoError(t, err)
}

func TestListOwnerResourcePools_QueryByServicePlanAndRegion(t *testing.T) {
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()

	strategyRepo := mockresourcepool.NewMockStrategyRepo(ctrl)
	repo := mockresourcepool.NewMockRepo(ctrl)

	s := &serviceImpl{
		resourcePoolRepo:         repo,
		resourcePoolStrategyRepo: strategyRepo,
	}

	repo.EXPECT().Query(gomock.Any(), gomock.Any()).DoAndReturn(
		func(_ context.Context, filter *reporesourcepool.Filter) ([]*entity.ResourcePool, int64, bizerr.BizError) {
			assert.Equal(t, []globalcommonv1.ServicePlan{globalcommonv1.ServicePlan_Essential_V2}, filter.ServicePlans)
			assert.Equal(t, []globalcommonv1.Region_Name{globalcommonv1.Region_AWS_US_EAST_1}, filter.RegionNames)
			assert.Equal(t, []globalcommonv1.CloudProvider_Name{globalcommonv1.CloudProvider_AWS}, filter.CloudProviderNames)
			return []*entity.ResourcePool{}, 0, nil
		},
	)
	strategyRepo.EXPECT().Query(gomock.Any(), gomock.Any()).DoAndReturn(
		func(_ context.Context, filter *reporesourcepool.StrategyFilter) ([]*entity.ResourcePoolStrategy, bizerr.BizError) {
			assert.Equal(t, []globalcommonv1.ServicePlan{globalcommonv1.ServicePlan_Essential_V2}, filter.ServicePlans)
			assert.Equal(t, []globalcommonv1.Region_Name{globalcommonv1.Region_AWS_US_EAST_1}, filter.RegionNames)
			assert.Equal(t, []globalcommonv1.CloudProvider_Name{globalcommonv1.CloudProvider_AWS}, filter.CloudProviderNames)
			return []*entity.ResourcePoolStrategy{}, nil
		},
	)

	_, _, err := s.ListOwnerResourcePools(context.Background(), &globalngv1.ListOwnerResourcePoolsRequest{
		OwnerType:   globalngv1.ResourcePool_ORGANIZATION,
		OwnerId:     100,
		ServicePlan: globalcommonv1.ServicePlan_Essential_V2,
		ProviderIds: []globalcommonv1.CloudProvider_Name{globalcommonv1.CloudProvider_AWS},
		RegionIds:   []globalcommonv1.Region_Name{globalcommonv1.Region_AWS_US_EAST_1},
	})

	assert.NoError(t, err)
}
