package services

import (
	"git.coopgo.io/coopgo-platform/geocode"
	routing "git.coopgo.io/coopgo-platform/routing-service"
	"github.com/rs/zerolog/log"
	"github.com/spf13/viper"
)

type ServicesHandler struct {
	MobilityAccounts MobilityAccountService
	Push             *PushService
	Geocoder         geocode.Geocoder
	Routing          routing.RoutingService
}

func NewServicesHandler(cfg *viper.Viper) (*ServicesHandler, error) {
	var (
		mobilityAccountsDial = cfg.GetString("services.internal.mobility_accounts.dial")
		pushDial             = cfg.GetString("services.internal.push.dial")
		geocoder_type        = cfg.GetString("geocoder.type")
		pelias_base_url      = cfg.GetString("geocoder.pelias.base_url")
		routing_service_type = cfg.GetString("routing.type")
		valhalla_base_url    = cfg.GetString("routing.valhalla.base_url")
	)
	mobilityAccounts, err := NewMobilityAccountService(mobilityAccountsDial)
	if err != nil {
		log.Fatal().Err(err).Msg("Could not connect to Mobility Accounts Service")
		return nil, err
	}
	push, err := NewPushService(pushDial)
	if err != nil {
		log.Fatal().Err(err).Msg("Could not connect to Push Notifications Service")
		return nil, err
	}
	geocoder, err := geocode.NewGeocoder(geocoder_type, pelias_base_url)
	if err != nil {
		log.Fatal().Err(err).Msg("Could not initiate the Geocoder service")
		return nil, err
	}

	routing, err := routing.NewRoutingService(routing_service_type, valhalla_base_url)
	if err != nil {
		log.Fatal().Err(err).Msg("Could not initiate the routing service")
		return nil, err
	}

	return &ServicesHandler{
		MobilityAccounts: mobilityAccounts,
		Push:             push,
		Geocoder: geocoder,
		Routing: routing,
	}, nil
}
