package audit

import (
	"fmt"

	"github.com/nspcc-dev/neofs-node/pkg/morph/client"
)

// GetAuditResultArgs groups the arguments
// of "get audit result" invocation call.
type GetAuditResultArgs struct {
	id []byte
}

// GetAuditResultValue groups the stack parameters
// returned by "get audit result" test invoke.
type GetAuditResultValue struct {
	rawResult []byte
}

// SetID sets audit result ID generated by audit contract.
func (g *GetAuditResultArgs) SetID(id []byte) {
	g.id = id
}

// Result returns audit result structure in binary format.
func (v *GetAuditResultValue) Result() []byte {
	return v.rawResult
}

// GetAuditResult invokes the call of "get audit result" method
// of NeoFS Audit contract.
func (c *Client) GetAuditResult(args GetAuditResultArgs) (*GetAuditResultValue, error) {
	prms, err := c.client.TestInvoke(
		c.getResultMethod,
		args.id,
	)
	if err != nil {
		return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.getResultMethod, err)
	} else if ln := len(prms); ln != 1 {
		return nil, fmt.Errorf("unexpected stack item count (%s): %d", c.getResultMethod, ln)
	}

	resultBytes, err := client.BytesFromStackItem(prms[0])
	if err != nil {
		return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", c.getResultMethod, err)
	}

	return &GetAuditResultValue{
		rawResult: resultBytes,
	}, nil
}