package output

// PartyRole represents the role type of a party in the MPC system
type PartyRole string

const (
	// PartyRolePersistent represents a persistent party that stores key shares in database
	// Used for normal keygen and sign sessions
	PartyRolePersistent PartyRole = "persistent"

	// PartyRoleCoManagedPersistent represents a persistent party for co_managed_keygen sessions
	// These parties use two-phase event handling (session_created -> session_started)
	// and are 100% compatible with service-party-app behavior
	PartyRoleCoManagedPersistent PartyRole = "co_managed_persistent"

	// PartyRoleDelegate represents a temporary party that generates user shares and returns them
	PartyRoleDelegate PartyRole = "delegate"

	// PartyRoleTemporary represents a temporary party for ad-hoc operations
	PartyRoleTemporary PartyRole = "temporary"
)

// NotificationChannel represents notification channels for a party
// If a party has any notification channel set, it operates in OFFLINE mode (24h async completion)
// If no notification channels are set, it operates in REAL-TIME mode (Message Router push)
type NotificationChannel struct {
	Email     string // Optional: email address for session invitations
	Phone     string // Optional: phone number for SMS notifications
	PushToken string // Optional: push notification token (FCM/APNs)
}

// HasAnyChannel returns true if any notification channel is configured
func (nc *NotificationChannel) HasAnyChannel() bool {
	return nc != nil && (nc.Email != "" || nc.Phone != "" || nc.PushToken != "")
}

// IsRealTimeMode returns true if party operates in real-time mode (no notification channels)
func (nc *NotificationChannel) IsRealTimeMode() bool {
	return !nc.HasAnyChannel()
}

// PartyEndpoint represents a party from the pool
// Note: Address is removed - parties connect to Message Router themselves
// Session Coordinator only needs PartyID for message routing
type PartyEndpoint struct {
	PartyID      string
	Ready        bool
	Role         PartyRole            // Role of the party (persistent, delegate, temporary)
	Notification *NotificationChannel // Optional: notification channels for offline mode
}

// IsRealTimeMode returns true if party operates in real-time mode
func (p *PartyEndpoint) IsRealTimeMode() bool {
	return p.Notification == nil || p.Notification.IsRealTimeMode()
}

// IsOfflineMode returns true if party operates in offline mode (has notification channels)
func (p *PartyEndpoint) IsOfflineMode() bool {
	return !p.IsRealTimeMode()
}

// PartySelectionFilter defines filtering criteria for party selection
type PartySelectionFilter struct {
	Count int         // Number of parties to select
	Role  PartyRole   // Required party role (empty string means any role)
}

// PartyPoolPort defines the interface for party pool management
type PartyPoolPort interface {
	// GetAvailableParties returns all available party endpoints
	GetAvailableParties() []PartyEndpoint

	// GetAvailablePartiesByRole returns available parties filtered by role
	GetAvailablePartiesByRole(role PartyRole) []PartyEndpoint

	// SelectParties selects n parties from the available pool
	SelectParties(n int) ([]PartyEndpoint, error)

	// SelectPartiesWithFilter selects parties based on filter criteria
	SelectPartiesWithFilter(filter PartySelectionFilter) ([]PartyEndpoint, error)
}
