package app

import (
	"fmt"

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

// GetTaxnexusCodeMap returns a map of all taxnexus codes
//
// Requires the presence of a "geo" service account in the
// application configuration file.
//
func GetTaxnexusCodeMap() (map[string]*TaxnexusCode, error) {
	sugar.Debug("ledger.GetTaxnexusCodeMap: 📥")
	geoUser := apiUser("geo")
	if geoUser == nil {
		return nil, fmt.Errorf("app.GetTaxnexusCodeMap: 💣⛔ cannot get auth, check config file")
	}
	params := taxnexus_code.NewGetTaxnexusCodesParams()
	getOK, err := geoClient.TaxnexusCode.GetTaxnexusCodes(params, geoUser.Auth)
	if err != nil {
		return nil, err
	}
	objMap := map[string]*TaxnexusCode{}
	n := 0
	for _, itm := range getOK.Payload.Data {
		n++
		objMap[itm.ID] = UnMarshalTaxnexusCode(itm)
		objMap[itm.Code] = objMap[itm.ID]
	}
	sugar.Infof("ledger.GetTaxnexusCodeMap: 📀 📤 %v taxnexus codes loaded", n)
	return objMap, nil
}
