package api

import (
	"testing"
	"time"
)

func TestJourneyEventUsesAuthenticatedUser(t *testing.T) {
	now := time.Date(2026, 8, 10, 12, 0, 0, 0, time.UTC)
	payload := journeyEventPayload{UserID: "user-1", JourneyID: "journey-1", Sequence: 3,
		Category: "navigation", Action: "open", Screen: "home", Feature: "search", Target: "search"}

	event, ok := toJourneyEvent(payload, "user-1", now)
	if !ok {
		t.Fatal("valid event was rejected")
	}
	if event.UserID != "user-1" || event.Sequence != 3 {
		t.Fatalf("unexpected event: %+v", event)
	}
	if _, ok := toJourneyEvent(payload, "user-2", now); ok {
		t.Fatal("an event buffered under another profile was accepted")
	}
}

func TestJourneyEventRejectsFreeTextAndUnknownVocabulary(t *testing.T) {
	now := time.Now().UTC()
	base := journeyEventPayload{UserID: "u1", JourneyID: "j1", Category: "content", Action: "open"}
	withTitle := base
	withTitle.Feature = "A Film Title"
	if _, ok := toJourneyEvent(withTitle, "u1", now); ok {
		t.Fatal("free text feature was accepted")
	}
	unknown := base
	unknown.Action = "typed_query"
	if _, ok := toJourneyEvent(unknown, "u1", now); ok {
		t.Fatal("unknown action was accepted")
	}
}
