package test

import (
	"context"
	"testing"

	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/suite"
	"github.com/tidbcloud/core-svc/idl/pbgen/proto/core"
	"github.com/tidbcloud/dataflow-service/pkg/grpc/grpcli"
	core_client_mock "github.com/tidbcloud/dataflow-service/pkg/grpc/grpcli/client-mock"
	"go.buf.build/protocolbuffers/go/linuxgit/tidbcloud/base"
	"gorm.io/gorm"
)

type coreTestSuite struct {
	suite.Suite
	db *gorm.DB

	mockCoreGrpcClient *core_client_mock.CoreGrpcClient
}

func TestGrpcCliTestSuite(t *testing.T) {
	suite.Run(t, new(coreTestSuite))
}

func (s *coreTestSuite) SetupSuite() {
	s.mockCoreGrpcClient = core_client_mock.NewCoreGrpcClient(s.T())
}

func (s *coreTestSuite) TestListClustersWithoutIds() {
	ctx := context.Background()
	clusters1 := make(map[uint64]*core.Cluster)
	for i := 0; i < 100; i++ {
		clusters1[uint64(i)] = &core.Cluster{Id: uint64(i)}
	}
	clusters2 := make(map[uint64]*core.Cluster)
	for i := 100; i < 199; i++ {
		clusters2[uint64(i)] = &core.Cluster{Id: uint64(i)}
	}
	req := &core.ListClustersReq{}
	resp1 := &core.ListClustersResp{
		Clusters: clusters1,
		BaseResp: &base.BaseResp{
			ErrCode: 0,
		},
	}
	resp2 := &core.ListClustersResp{
		Clusters: clusters2,
		BaseResp: &base.BaseResp{
			ErrCode: 0,
		},
	}
	s.mockCoreGrpcClient.On("ListClusters", mock.Anything, mock.Anything).Return(resp1, nil).Once()
	s.mockCoreGrpcClient.On("ListClusters", mock.Anything, mock.Anything).Return(resp2, nil).Once()

	core := &grpcli.CoreGrpcClientImpl{
		Client: s.mockCoreGrpcClient,
	}
	resp, err := core.ListClustersWithoutIds(ctx, req)

	s.Nil(err)
	s.Equal(199, len(resp.Clusters))
}
