package grpcli

import (
	"context"

	"github.com/tidbcloud/dataflow-service/idl/pbgen/proto/dataflow"
	"github.com/tidbcloud/dataflow-service/pkg/log"
	"github.com/tidbcloud/pkgv2/bizerr"
	"go.uber.org/zap"
)

type ImportGrpcClient interface {
	GetClient() dataflow.ImportInnerServiceClient

	List(ctx context.Context, req *dataflow.ListImportReq) (*dataflow.ListImportResp, bizerr.BizError)
}

type importGrpcClientImpl struct {
	addr   string
	client dataflow.ImportInnerServiceClient
}

func NewImportGrpcClient(ctx context.Context, addr string) ImportGrpcClient {
	logger := log.GetLogger(ctx)
	logger.Info("initializing import client", zap.String("addr", addr))

	conn := MustBuildGrpcClientConnection(ctx, addr)
	coreCli := dataflow.NewImportInnerServiceClient(conn)
	return &importGrpcClientImpl{
		addr:   addr,
		client: coreCli,
	}
}

func (c *importGrpcClientImpl) GetClient() dataflow.ImportInnerServiceClient {
	return c.client
}

func (c *importGrpcClientImpl) List(ctx context.Context, req *dataflow.ListImportReq,
) (*dataflow.ListImportResp, bizerr.BizError) {
	resp, e := c.client.List(ctx, req)
	if e != nil {
		return nil, bizerr.WrapGrpcError(e, "ListImport")
	}
	return resp, nil
}
