package container

import (
	"fmt"

	"git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/container"
	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
	cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
	"github.com/nspcc-dev/neo-go/pkg/core/state"
	"github.com/nspcc-dev/neo-go/pkg/network/payload"
)

// Delete structure of container.Delete notification from morph chain.
type Delete struct {
	ContainerIDValue []byte
	SignatureValue   []byte
	TokenValue       []byte
	PublicKeyValue   []byte

	// For notary notifications only.
	// Contains raw transactions of notary request.
	NotaryRequestValue *payload.P2PNotaryRequest
}

// MorphEvent implements Neo:Morph Event interface.
func (Delete) MorphEvent() {}

// ContainerID is a marshalled container structure, defined in API.
func (d Delete) ContainerID() []byte { return d.ContainerIDValue }

// Signature of marshalled container by container owner.
func (d Delete) Signature() []byte { return d.SignatureValue }

// SessionToken returns binary token of the session
// within which the eACL was set.
func (d Delete) SessionToken() []byte {
	return d.TokenValue
}

// NotaryRequest returns raw notary request if notification
// was received via notary service. Otherwise, returns nil.
func (d Delete) NotaryRequest() *payload.P2PNotaryRequest {
	return d.NotaryRequestValue
}

const expectedItemNumDelete = 4

// DeleteSuccess structures notification event of successful container removal
// thrown by Container contract.
type DeleteSuccess struct {
	// Identifier of the removed container.
	ID cid.ID
}

// MorphEvent implements Neo:Morph Event interface.
func (DeleteSuccess) MorphEvent() {}

// ParseDeleteSuccess decodes notification event thrown by Container contract into
// DeleteSuccess and returns it as event.Event.
func ParseDeleteSuccess(e *state.ContainedNotificationEvent) (event.Event, error) {
	var dse container.DeleteSuccessEvent
	if err := dse.FromStackItem(e.Item); err != nil {
		return nil, fmt.Errorf("parse container.DeleteSuccessEvent: %w", err)
	}

	var cnr cid.ID
	cnr.SetSHA256(dse.ContainerID)
	return DeleteSuccess{
		ID: cnr,
	}, nil
}