package service

import "testing"

func TestParseSESEventNotification(t *testing.T) {
	t.Run("parses direct SES event payload", func(t *testing.T) {
		event, err := ParseSESEventNotification([]byte(`{
			"eventType":"Delivery",
			"mail":{"messageId":"ses-msg-1","source":"no-reply@example.com","destination":["user@example.com"]},
			"delivery":{"processingTimeMillis":123}
		}`))
		if err != nil {
			t.Fatalf("parse: %v", err)
		}
		if event.EventType != "Delivery" || event.Mail.MessageID != "ses-msg-1" {
			t.Fatalf("unexpected event: %+v", event)
		}
		if event.Delivery == nil || event.Delivery.ProcessingTimeMillis != 123 {
			t.Fatalf("expected delivery details, got %+v", event.Delivery)
		}
	})

	t.Run("parses SNS-wrapped SES event payload", func(t *testing.T) {
		event, err := ParseSESEventNotification([]byte(`{
			"Type":"Notification",
			"Message":"{\"eventType\":\"Bounce\",\"mail\":{\"messageId\":\"ses-msg-2\",\"source\":\"no-reply@example.com\",\"destination\":[\"user@example.com\"]},\"bounce\":{\"bounceType\":\"Permanent\"}}"
		}`))
		if err != nil {
			t.Fatalf("parse: %v", err)
		}
		if event.EventType != "Bounce" || event.Mail.MessageID != "ses-msg-2" {
			t.Fatalf("unexpected event: %+v", event)
		}
		if event.Bounce == nil || event.Bounce.BounceType != "Permanent" {
			t.Fatalf("expected bounce details, got %+v", event.Bounce)
		}
	})

	t.Run("rejects missing event type", func(t *testing.T) {
		if _, err := ParseSESEventNotification([]byte(`{"mail":{"messageId":"ses-msg-3"}}`)); err == nil {
			t.Fatalf("expected error for missing event type")
		}
	})
}
