package hostruntime

import "context"

const FeatureWebSocket = "FEATURE_WEBSOCKET"

// FeatureGate reports whether a runtime feature has been disabled.
type FeatureGate interface {
	Disabled(feature string) bool
}

// WebSocketSessionStore is the host-runtime session registry needed by the
// WebSocket transport.
type WebSocketSessionStore interface {
	CreateSession() string
	DestroySession(clientID string)
	Subscribe(clientID, channel string) bool
	Unsubscribe(clientID, channel string) bool
	GetSubscribedChannels(clientID string) []string
}

// WebSocketAuthorizationOperation identifies a protected long-lived transport
// boundary. Reauthorization is required before the corresponding session
// mutation or delivery occurs.
type WebSocketAuthorizationOperation uint8

const (
	WebSocketAuthorizeSubscribe WebSocketAuthorizationOperation = iota + 1
	WebSocketAuthorizeUnsubscribe
	WebSocketAuthorizeDisconnect
	WebSocketAuthorizeDelivery
)

// Valid reports whether operation is part of the closed WebSocket contract.
func (operation WebSocketAuthorizationOperation) Valid() bool {
	switch operation {
	case WebSocketAuthorizeSubscribe,
		WebSocketAuthorizeUnsubscribe,
		WebSocketAuthorizeDisconnect,
		WebSocketAuthorizeDelivery:
		return true
	default:
		return false
	}
}

// WebSocketAuthorizationRequest describes one protected connection boundary.
// Channel is the subscribed or delivered application channel and is empty for
// disconnect.
type WebSocketAuthorizationRequest struct {
	Operation WebSocketAuthorizationOperation
	Channel   string
}

// WebSocketAuthorizationLease re-establishes current credential, subject and
// Permission State authority for one open connection. Implementations return
// ErrAuthorizingCredentialRejected only when the credential is no longer
// valid; every other error is an authority failure. Implementations must honor
// context cancellation so their own work and resources are also released; the
// host independently enforces its caller-facing return boundary.
type WebSocketAuthorizationLease interface {
	ReauthorizeWebSocket(
		ctx context.Context,
		request WebSocketAuthorizationRequest,
	) (AuthorizingPermissionState, error)
}

// WebSocketAuthorizationLeaseFactory opens one connection-scoped
// reauthorization lease from the already admitted bearer and Permission State.
// Implementations must never log the bearer and should retain only the minimum
// opaque credential handle needed for later strong re-admission. Implementations
// must honor context cancellation; the host independently enforces its
// caller-facing return boundary.
type WebSocketAuthorizationLeaseFactory interface {
	OpenWebSocketAuthorization(
		ctx context.Context,
		bearerToken string,
		initial AuthorizingPermissionState,
	) (WebSocketAuthorizationLease, error)
}
