package discovery

import (
	"strings"

	me "github.com/tech/sendico/pkg/messaging/envelope"
	"go.uber.org/zap"
)

func announcementFields(announce Announcement) []zap.Field {
	fields := make([]zap.Field, 0, 10)
	if announce.ID != "" {
		fields = append(fields, zap.String("id", announce.ID))
	}
	if announce.InstanceID != "" {
		fields = append(fields, zap.String("instance_id", announce.InstanceID))
	}
	if announce.Service != "" {
		fields = append(fields, zap.String("service", announce.Service))
	}
	if announce.Rail != "" {
		fields = append(fields, zap.String("rail", announce.Rail))
	}
	if network := legacyNetworkFromCurrencies(announce.Currencies); network != "" {
		fields = append(fields, zap.String("network", network))
	}
	if announce.InvokeURI != "" {
		fields = append(fields, zap.String("invoke_uri", announce.InvokeURI))
	}
	if announce.Version != "" {
		fields = append(fields, zap.String("version", announce.Version))
	}
	if announce.RoutingPriority != 0 {
		fields = append(fields, zap.Int("routing_priority", announce.RoutingPriority))
	}
	if len(announce.Operations) > 0 {
		fields = append(fields, zap.Int("ops", len(announce.Operations)))
	}
	if len(announce.Currencies) > 0 {
		fields = append(fields, zap.Int("currencies", len(announce.Currencies)))
	}
	if announce.Health.IntervalSec > 0 {
		fields = append(fields, zap.Int("interval_seconds", announce.Health.IntervalSec))
	}
	if announce.Health.TimeoutSec > 0 {
		fields = append(fields, zap.Int("timeout_seconds", announce.Health.TimeoutSec))
	}
	return fields
}

func entryFields(entry RegistryEntry) []zap.Field {
	fields := make([]zap.Field, 0, 12)
	if entry.ID != "" {
		fields = append(fields, zap.String("id", entry.ID))
	}
	if entry.InstanceID != "" {
		fields = append(fields, zap.String("instance_id", entry.InstanceID))
	}
	if entry.Service != "" {
		fields = append(fields, zap.String("service", entry.Service))
	}
	if entry.Rail != "" {
		fields = append(fields, zap.String("rail", entry.Rail))
	}
	if entry.Network != "" {
		fields = append(fields, zap.String("network", entry.Network))
	}
	if entry.Version != "" {
		fields = append(fields, zap.String("version", entry.Version))
	}
	if entry.InvokeURI != "" {
		fields = append(fields, zap.String("invoke_uri", entry.InvokeURI))
	}
	if entry.Status != "" {
		fields = append(fields, zap.String("status", entry.Status))
	}
	if !entry.LastHeartbeat.IsZero() {
		fields = append(fields, zap.Time("last_heartbeat", entry.LastHeartbeat))
	}
	fields = append(fields, zap.Bool("healthy", entry.Healthy))
	if entry.RoutingPriority != 0 {
		fields = append(fields, zap.Int("routing_priority", entry.RoutingPriority))
	}
	if len(entry.Operations) > 0 {
		fields = append(fields, zap.Int("ops", len(entry.Operations)))
	}
	if len(entry.Currencies) > 0 {
		fields = append(fields, zap.Int("currencies", len(entry.Currencies)))
	}
	return fields
}

func envelopeFields(env me.Envelope) []zap.Field {
	if env == nil {
		return nil
	}
	fields := make([]zap.Field, 0, 4)
	sender := strings.TrimSpace(env.GetSender())
	if sender != "" {
		fields = append(fields, zap.String("sender", sender))
	}
	if signature := env.GetSignature(); signature != nil {
		fields = append(fields, zap.String("event", signature.ToString()))
	}
	fields = append(fields, zap.String("message_id", env.GetMessageId().String()))
	fields = append(fields, zap.Time("timestamp", env.GetTimeStamp()))
	return fields
}
