package app

import (
	"code.tnxs.net/taxnexus/lib/api/auth/auth_client"
	"code.tnxs.net/taxnexus/lib/api/auth/auth_client/user"
	"code.tnxs.net/taxnexus/lib/api/auth/auth_models"
)

var authClient = auth_client.Default

// CheckAPIUser is exported
func CheckAPIUser(token *string) (*User, error) {
	sugar.Debug("app.CheckAPIUser: 📥")
	params := user.NewGetUsersParams()
	params.Apikey = token
	response, err := authClient.User.GetUsers(params, apiUsers["auth"].Auth)
	if err != nil {
		sugar.Warnf("app.CheckAPIUser: ❗ Access attempt with invalid API key: %s", *token)
		return nil, err
	}
	var obj *auth_models.User
	for _, itm := range response.Payload.Data { // single iteration execution
		obj = itm
	}
	sugar.Debugf("app.CheckAPIUser: 📤 👍 ID = %s, Name = %s", obj.ID, obj.Name)
	return MarshalAuthUserToSwagger(obj), nil
}
