package social

import (
	"context"
	"testing"

	"go.opentelemetry.io/otel/attribute"
	sdkmetric "go.opentelemetry.io/otel/sdk/metric"
	"go.opentelemetry.io/otel/sdk/metric/metricdata"
)

// TestSocialMetrics records chat and nudge entries through a manual reader and
// asserts chat_messages_total splits by the "kind" attribute.
func TestSocialMetrics(t *testing.T) {
	ctx := context.Background()
	reader := sdkmetric.NewManualReader()
	meter := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)).Meter("test")
	m := newSocialMetrics(meter)

	m.recordChat(ctx, kindMessage)
	m.recordChat(ctx, kindMessage)
	m.recordChat(ctx, kindNudge)

	var rm metricdata.ResourceMetrics
	if err := reader.Collect(ctx, &rm); err != nil {
		t.Fatalf("collect: %v", err)
	}

	got := map[string]int64{}
	for _, sm := range rm.ScopeMetrics {
		for _, md := range sm.Metrics {
			if md.Name != "chat_messages_total" {
				continue
			}
			sum, ok := md.Data.(metricdata.Sum[int64])
			if !ok {
				t.Fatalf("chat_messages_total is not an int64 sum")
			}
			for _, dp := range sum.DataPoints {
				v, _ := dp.Attributes.Value(attribute.Key("kind"))
				got[v.AsString()] += dp.Value
			}
		}
	}
	if got[kindMessage] != 2 || got[kindNudge] != 1 {
		t.Errorf("chat_messages_total = %v, want message:2 nudge:1", got)
	}
}
