package quotation

import (
	"context"
	"net"
	"testing"

	"github.com/tech/sendico/pkg/api/routers"
	quotationv2 "github.com/tech/sendico/pkg/proto/payments/quotation/v2"
	"go.uber.org/zap"
	"google.golang.org/grpc"
)

func TestQuotationService_RegisterV2Only(t *testing.T) {
	svc := NewQuotationService(zap.NewNop(), nil)
	router := newGRPCCaptureRouter()

	if err := svc.Register(router); err != nil {
		t.Fatalf("Register returned error: %v", err)
	}

	services := router.server.GetServiceInfo()
	if _, ok := services[quotationv2.QuotationService_ServiceDesc.ServiceName]; !ok {
		t.Fatalf("expected %q service to be registered", quotationv2.QuotationService_ServiceDesc.ServiceName)
	}
	if len(services) != 1 {
		t.Fatalf("expected exactly one registered service, got %d", len(services))
	}
}

type grpcCaptureRouter struct {
	server *grpc.Server
	done   chan error
}

func newGRPCCaptureRouter() *grpcCaptureRouter {
	return &grpcCaptureRouter{
		server: grpc.NewServer(),
		done:   make(chan error),
	}
}

func (r *grpcCaptureRouter) Register(registration routers.GRPCServiceRegistration) error {
	registration(r.server)
	return nil
}

func (r *grpcCaptureRouter) Start(context.Context) error { return nil }

func (r *grpcCaptureRouter) Finish(context.Context) error { return nil }

func (r *grpcCaptureRouter) Addr() net.Addr { return nil }

func (r *grpcCaptureRouter) Done() <-chan error { return r.done }

var _ routers.GRPC = (*grpcCaptureRouter)(nil)
