package eventbus

import (
	"context"
	"log/slog"

	"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
)

type LogSink struct {
	logger *slog.Logger
}

func NewLogSink(logger *slog.Logger) *LogSink {
	if logger == nil {
		logger = slog.Default()
	}
	return &LogSink{logger: logger}
}

func (s *LogSink) PublishRaw(_ context.Context, env envelope.FrameEnvelope) error {
	s.logger.Info("raw envelope", "protocol", env.Protocol, "event_id", env.StableEventID(), "vehicle_key", env.VehicleKey(), "message_id", env.MessageID, "status", env.ParseStatus)
	return nil
}

func (s *LogSink) PublishUnified(_ context.Context, env envelope.FrameEnvelope) error {
	s.logger.Info("unified envelope", "protocol", env.Protocol, "event_id", env.StableEventID(), "vehicle_key", env.VehicleKey(), "message_id", env.MessageID)
	return nil
}

func (s *LogSink) PublishFields(_ context.Context, env envelope.FrameEnvelope) error {
	s.logger.Info("fields envelope", "protocol", env.Protocol, "event_id", env.StableEventID(), "vehicle_key", env.VehicleKey(), "message_id", env.MessageID, "field_count", len(env.Fields))
	return nil
}

func (s *LogSink) Close() error {
	return nil
}
