package orchestrator

import (
	"strings"

	"github.com/tech/sendico/pkg/merrors"
	"go.uber.org/zap"
)

func (s *Service) cardRoute(gateway string) (CardGatewayRoute, error) {
	if len(s.deps.cardRoutes) == 0 {
		s.logger.Warn("card routing not configured", zap.String("gateway", gateway))
		return CardGatewayRoute{}, merrors.InvalidArgument("card routing not configured")
	}
	key := strings.ToLower(strings.TrimSpace(gateway))
	if key == "" {
		key = defaultCardGateway
	}
	route, ok := s.deps.cardRoutes[key]
	if !ok {
		s.logger.Warn("card routing missing for gateway", zap.String("gateway", key))
		return CardGatewayRoute{}, merrors.InvalidArgument("card routing missing for gateway " + key)
	}
	if strings.TrimSpace(route.FundingAddress) == "" {
		s.logger.Warn("card routing missing funding address", zap.String("gateway", key))
		return CardGatewayRoute{}, merrors.InvalidArgument("card funding address is required for gateway " + key)
	}
	return route, nil
}
