package service

import (
	"nex/backend/internal/domain"
	appErrors "nex/backend/pkg/errors"
)

type routingService struct {
	cache *RoutingCache
}

func NewRoutingService(cache *RoutingCache) RoutingService {
	return &routingService{cache: cache}
}

func (s *routingService) RouteByModelName(providerID, modelName string) (*domain.RouteResult, error) {
	model, err := s.cache.GetModel(providerID, modelName)
	if err != nil {
		return nil, appErrors.ErrModelNotFound
	}

	if !model.Enabled {
		return nil, appErrors.ErrModelDisabled
	}

	provider, err := s.cache.GetProvider(model.ProviderID)
	if err != nil {
		return nil, appErrors.ErrProviderNotFound
	}

	if !provider.Enabled {
		return nil, appErrors.ErrProviderDisabled
	}

	return &domain.RouteResult{
		Provider: provider,
		Model:    model,
	}, nil
}
