package subnetevents

import (
	"fmt"

	subnetid "github.com/nspcc-dev/neofs-sdk-go/subnet/id"
)

// Delete represents a notification about NeoFS subnet removal.
// Generated by a contract when intending to delete a subnet.
type Delete interface {
	// Contains the ID of the subnet to be removed.
	eventWithID
}

// DeleteValidator asserts intent to remove a subnet.
type DeleteValidator struct{}

// Assert processes the attempt to remove a subnet. It approves the removal through nil return.
//
// All read errors of Delete are forwarded.
//
// It returns an error on:
//   * zero subnet creation;
//   * empty ID or different from the one wired into info;
//   * empty owner ID or different from the one wired into info.
func (x DeleteValidator) Assert(event Delete) error {
	var err error

	// read ID
	var id subnetid.ID
	if err = event.ReadID(&id); err != nil {
		return fmt.Errorf("read ID: %w", err)
	}

	// prevent zero subnet removal
	if subnetid.IsZero(id) {
		return zeroSubnetOp{
			op: "removal",
		}
	}

	return nil
}