package model

import (
	"encoding/json"
	"testing"
)

func TestAdventureLogLine_JSON_roundTrip(t *testing.T) {
	line := AdventureLogLine{
		Message: "legacy",
		Event: &AdventureLogEvent{
			Code: LogPhraseDefeatedEnemy,
			Args: map[string]any{"enemyType": "wolf_l1_1_meadow", "xp": float64(10), "gold": float64(5)},
		},
	}
	b, err := json.Marshal(line)
	if err != nil {
		t.Fatal(err)
	}
	var got AdventureLogLine
	if err := json.Unmarshal(b, &got); err != nil {
		t.Fatal(err)
	}
	if got.Message != line.Message {
		t.Fatalf("message: got %q want %q", got.Message, line.Message)
	}
	if got.Event == nil || got.Event.Code != LogPhraseDefeatedEnemy {
		t.Fatalf("event code: %+v", got.Event)
	}
	if got.Event.Args["enemyType"] != "wolf_l1_1_meadow" {
		t.Fatalf("args: %+v", got.Event.Args)
	}
}

func TestAdventureLogLine_JSON_legacyMessageOnly(t *testing.T) {
	raw := `{"message":"hello"}`
	var got AdventureLogLine
	if err := json.Unmarshal([]byte(raw), &got); err != nil {
		t.Fatal(err)
	}
	if got.Message != "hello" || got.Event != nil {
		t.Fatalf("got %+v", got)
	}
}
