package app

import (
	"fmt"

	"code.tnxs.net/taxnexus/lib/api/geo/geo_client/domain"
)

// GetDomainMap returns a map of all domains
//
// Requires the presence of a "geo" service account in the
// application configuration file.
//
func GetDomainMap() (map[string]*Domain, error) {
	sugar.Debug("ledger.GetDomainMap: 📥")
	geoUser := apiUser("geo")
	if geoUser == nil {
		return nil, fmt.Errorf("app.GetDomainMap: 💣⛔ cannot get auth, check config file")
	}
	params := domain.NewGetDomainsParamsWithTimeout(getTimeout)
	getOK, err := geoClient.Domain.GetDomains(params, geoUser.Auth)
	if err != nil {
		return nil, err
	}
	objMap := map[string]*Domain{}
	for _, itm := range getOK.Payload.Data {
		objMap[itm.ID] = UnMarshalDomain(itm)
	}
	sugar.Infof("ledger.GetDomainMap: 📀 📤 %v domains loaded", len(objMap))
	return objMap, nil
}
