package realtime

import (
	"encoding/json"
	"testing"
)

func TestEventTypeConstants(t *testing.T) {
	tests := map[string]string{
		"mail.created":    TypeMailCreated,
		"mail.updated":    TypeMailUpdated,
		"mail.deleted":    TypeMailDeleted,
		"outbox.updated":  TypeOutboxUpdated,
		"contact.updated": TypeContactUpdated,
		"ws.ping":         TypeWSPing,
		"ws.pong":         TypeWSPong,
	}
	for want, got := range tests {
		if got != want {
			t.Errorf("constant = %q, want %q", got, want)
		}
	}
}

func TestMailEventConstructorsJSON(t *testing.T) {
	tests := []struct {
		name  string
		event Event
		want  string
	}{
		{
			name:  "created",
			event: NewMailCreatedEvent("msg-1", "acct-1"),
			want:  `{"type":"mail.created","payload":{"message_id":"msg-1","account_id":"acct-1"}}`,
		},
		{
			name:  "updated",
			event: NewMailUpdatedEvent("msg-2", "acct-2"),
			want:  `{"type":"mail.updated","payload":{"message_id":"msg-2","account_id":"acct-2"}}`,
		},
		{
			name:  "deleted",
			event: NewMailDeletedEvent("msg-3", "acct-3"),
			want:  `{"type":"mail.deleted","payload":{"message_id":"msg-3","account_id":"acct-3"}}`,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := json.Marshal(tt.event)
			if err != nil {
				t.Fatalf("json.Marshal() error = %v", err)
			}
			if string(got) != tt.want {
				t.Fatalf("json = %s, want %s", got, tt.want)
			}
		})
	}
}

func TestOutboxAndContactEventConstructorsJSON(t *testing.T) {
	outbox, err := json.Marshal(NewOutboxUpdatedEvent("acct-1"))
	if err != nil {
		t.Fatalf("json.Marshal(outbox) error = %v", err)
	}
	if string(outbox) != `{"type":"outbox.updated","payload":{"account_id":"acct-1"}}` {
		t.Fatalf("outbox json = %s", outbox)
	}

	contact, err := json.Marshal(NewContactUpdatedEvent("contact-1", "acct-1"))
	if err != nil {
		t.Fatalf("json.Marshal(contact) error = %v", err)
	}
	if string(contact) != `{"type":"contact.updated","payload":{"contact_id":"contact-1","account_id":"acct-1"}}` {
		t.Fatalf("contact json = %s", contact)
	}
}
