package logging

import (
	"log/slog"
	"testing"
	"time"
)

func TestRecentStoreListNewestFirst(t *testing.T) {
	t.Parallel()

	store := NewRecentStore(3)
	store.Add(Entry{Message: "first", Timestamp: time.Unix(1, 0)})
	store.Add(Entry{Message: "second", Timestamp: time.Unix(2, 0)})
	store.Add(Entry{Message: "third", Timestamp: time.Unix(3, 0)})
	store.Add(Entry{Message: "fourth", Timestamp: time.Unix(4, 0)})

	got := store.List(2)
	if len(got) != 2 {
		t.Fatalf("expected 2 entries, got %d", len(got))
	}
	if got[0].Message != "fourth" || got[1].Message != "third" {
		t.Fatalf("unexpected order: %+v", got)
	}
}

func TestRecentHandlerCapturesFields(t *testing.T) {
	t.Parallel()

	store := NewRecentStore(5)
	h := NewRecentHandler(store, slog.LevelInfo)
	logger := slog.New(h)
	logger.Info("test event", "component", "autofill", "suggestion_count", 7)

	items := store.List(1)
	if len(items) != 1 {
		t.Fatalf("expected 1 entry, got %d", len(items))
	}
	if items[0].Fields["component"] != "autofill" {
		t.Fatalf("missing component field: %+v", items[0].Fields)
	}
	if items[0].Fields["suggestion_count"] != int64(7) {
		t.Fatalf("missing suggestion_count field: %+v", items[0].Fields)
	}
}
