package events

import (
	"time"

	"github.com/google/uuid"

	iop "iop/proto/gen/iop"
)

const (
	SourceEdge = "edge"
	SourceNode = "node"

	TypeNodeConnected          = "node.connected"
	TypeNodeDisconnected       = "node.disconnected"
	TypeEdgeDisconnected       = "edge.disconnected"
	TypeNodeOnline             = "node.online"
	TypeNodeRegistrationFailed = "node.registration_failed"

	ReasonRegistered          = "registered"
	ReasonTransportClosed     = "transport_closed"
	ReasonLocalShutdown       = "local_shutdown"
	ReasonEdgeShutdown        = "edge_shutdown"
	ReasonFirstHeartbeat      = "first_heartbeat"
	ReasonUnknownToken        = "unknown_token"
	ReasonDuplicateConnection = "duplicate_connection"
	ReasonHeartbeatTimeout    = "heartbeat_timeout"

	MetadataTransportCloseReason = "transport_close_reason"
	MetadataTransportCloseError  = "transport_close_error"
	MetadataLifecycleState       = "lifecycle_state"
	MetadataFailureReason        = "failure_reason"
	MetadataTokenPrefix          = "token_prefix"
)

func NewEdgeNodeEvent(source, eventType, nodeID, alias, reason string, metadata map[string]string) *iop.EdgeNodeEvent {
	return &iop.EdgeNodeEvent{
		EventId:   uuid.NewString(),
		Type:      eventType,
		Source:    source,
		NodeId:    nodeID,
		Alias:     alias,
		Reason:    reason,
		Metadata:  metadata,
		Timestamp: time.Now().UnixNano(),
	}
}
