package accntcli

import (
	"context"
	"encoding/json"
	"errors"

	"mstore/app/accoper"
	"mstore/app/handler"
	"mstore/pkg/descr"
)

func (cli *Client) GetGrant(ctx context.Context, host, grantID string) (*descr.Grant, error) {
	var err error
	res := &descr.Grant{}
	params := accoper.GetGrantParams{
		GrantID: grantID,
	}
	reqdata, err := json.Marshal(params)
	if err != nil {
		return res, err
	}
	respdata, err := cli.doHTTPCall(ctx, host, "grant", "get", reqdata)
	if err != nil {
		return res, err
	}

	response := handler.NewResponse[accoper.GetGrantResult]()
	err = json.Unmarshal(respdata, response)
	if err != nil {
		return res, err
	}
	if response.Error {
		err = errors.New(response.Message)
		return res, err
	}
	res = response.Result.Grant
	return res, err
}
