package accntcli

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

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

func (cli *Client) DeleteGrant(ctx context.Context, host, grantID string) error {
	var err error
	params := accoper.DeleteGrantParams{
		GrantID: grantID,
	}
	reqdata, err := json.Marshal(params)
	if err != nil {
		return err
	}
	respdata, err := cli.doHTTPCall(ctx, host, "grant", "delete", reqdata)
	if err != nil {
		return err
	}

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