package quotation

import (
	"github.com/tech/sendico/payments/quotation/internal/service/quotation/quotation_service_v2"
	"github.com/tech/sendico/payments/storage"
	"github.com/tech/sendico/pkg/api/routers"
	"github.com/tech/sendico/pkg/mlogger"
	quotationv2 "github.com/tech/sendico/pkg/proto/payments/quotation/v2"
	"google.golang.org/grpc"
)

// QuotationService exposes quotation-v2 RPCs as a standalone gRPC service.
type QuotationService struct {
	core *Service
	v2   *quotation_service_v2.QuotationServiceV2
}

// NewQuotationService constructs a standalone quotation service.
func NewQuotationService(logger mlogger.Logger, repo storage.Repository, opts ...Option) *QuotationService {
	core := NewService(logger, repo, opts...)
	return &QuotationService{
		core: core,
		v2:   newQuotationServiceV2(core),
	}
}

// Register attaches only the quotation service to the supplied gRPC router.
func (s *QuotationService) Register(router routers.GRPC) error {
	if s == nil || s.v2 == nil {
		return nil
	}
	return router.Register(func(reg grpc.ServiceRegistrar) {
		quotationv2.RegisterQuotationServiceServer(reg, s.v2)
	})
}

// Shutdown releases resources used by the underlying core service.
func (s *QuotationService) Shutdown() {
	if s != nil && s.core != nil {
		s.core.Shutdown()
	}
}
