package container

import (
	"fmt"

	"github.com/TrueCloudLab/frostfs-node/pkg/morph/client"
	"github.com/TrueCloudLab/frostfs-node/pkg/morph/event"
	cid "github.com/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 {
	containerID []byte
	signature   []byte
	token       []byte

	// For notary notifications only.
	// Contains raw transactions of notary request.
	notaryRequest *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.containerID }

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

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

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

const expectedItemNumDelete = 3

// ParseDelete from notification into container event structure.
//
// Expects 3 stack items.
func ParseDelete(e *state.ContainedNotificationEvent) (event.Event, error) {
	var (
		ev  Delete
		err error
	)

	params, err := event.ParseStackArray(e)
	if err != nil {
		return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
	}

	if ln := len(params); ln != expectedItemNumDelete {
		return nil, event.WrongNumberOfParameters(expectedItemNumDelete, ln)
	}

	// parse container
	ev.containerID, err = client.BytesFromStackItem(params[0])
	if err != nil {
		return nil, fmt.Errorf("could not get container: %w", err)
	}

	// parse signature
	ev.signature, err = client.BytesFromStackItem(params[1])
	if err != nil {
		return nil, fmt.Errorf("could not get signature: %w", err)
	}

	// parse session token
	ev.token, err = client.BytesFromStackItem(params[2])
	if err != nil {
		return nil, fmt.Errorf("could not get session token: %w", err)
	}

	return ev, nil
}

// 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) {
	items, err := event.ParseStackArray(e)
	if err != nil {
		return nil, fmt.Errorf("parse stack array from raw notification event: %w", err)
	}

	const expectedItemNumDeleteSuccess = 1

	if ln := len(items); ln != expectedItemNumDeleteSuccess {
		return nil, event.WrongNumberOfParameters(expectedItemNumDeleteSuccess, ln)
	}

	binID, err := client.BytesFromStackItem(items[0])
	if err != nil {
		return nil, fmt.Errorf("parse container ID item: %w", err)
	}

	var res DeleteSuccess

	err = res.ID.Decode(binID)
	if err != nil {
		return nil, fmt.Errorf("decode container ID: %w", err)
	}

	return res, nil
}