package accntcli

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

	"mstore/app/accoper"
	"mstore/app/handler"
)

func (cli *Client) UpdateGrant(ctx context.Context, host, grantID, newPattern string) error {
	var err error
	params := accoper.UpdateGrantParams{
		GrantID:    grantID,
		NewPattern: newPattern,
	}
	reqdata, err := json.Marshal(params)
	if err != nil {
		return err
	}
	respdata, err := cli.doHTTPCall(ctx, host, "grant", "update", reqdata)
	if err != nil {
		return err
	}
	response := handler.NewResponse[accoper.UpdateGrantResult]()
	err = json.Unmarshal(respdata, response)
	if err != nil {
		return err
	}
	if response.Error {
		err = errors.New(response.Message)
		return err
	}
	return err
}
