package worker

import (
	"encoding/json"
	"testing"
)

// --- Payload roundtrip tests ---

func TestWelcomeEmailPayload_MarshalRoundtrip(t *testing.T) {
	original := WelcomeEmailPayload{
		EmailPayload:     EmailPayload{To: "a@b.com", FirstName: "Alice"},
		ConfirmationCode: "ABC123",
	}
	data, err := json.Marshal(original)
	if err != nil {
		t.Fatalf("marshal: %v", err)
	}
	var got WelcomeEmailPayload
	if err := json.Unmarshal(data, &got); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if got.To != original.To || got.FirstName != original.FirstName || got.ConfirmationCode != original.ConfirmationCode {
		t.Errorf("roundtrip mismatch: got %+v, want %+v", got, original)
	}
}

func TestVerificationEmailPayload_MarshalRoundtrip(t *testing.T) {
	original := VerificationEmailPayload{
		EmailPayload: EmailPayload{To: "b@c.com", FirstName: "Bob"},
		Code:         "999888",
	}
	data, err := json.Marshal(original)
	if err != nil {
		t.Fatalf("marshal: %v", err)
	}
	var got VerificationEmailPayload
	if err := json.Unmarshal(data, &got); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if got.To != original.To || got.FirstName != original.FirstName || got.Code != original.Code {
		t.Errorf("roundtrip mismatch: got %+v, want %+v", got, original)
	}
}

func TestPasswordResetEmailPayload_MarshalRoundtrip(t *testing.T) {
	original := PasswordResetEmailPayload{
		EmailPayload: EmailPayload{To: "c@d.com", FirstName: "Carol"},
		Code:         "XYZ",
		ResetToken:   "tok-abc-123",
	}
	data, err := json.Marshal(original)
	if err != nil {
		t.Fatalf("marshal: %v", err)
	}
	var got PasswordResetEmailPayload
	if err := json.Unmarshal(data, &got); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if got.To != original.To || got.FirstName != original.FirstName || got.Code != original.Code || got.ResetToken != original.ResetToken {
		t.Errorf("roundtrip mismatch: got %+v, want %+v", got, original)
	}
}

func TestPasswordChangedEmailPayload_MarshalRoundtrip(t *testing.T) {
	original := EmailPayload{To: "d@e.com", FirstName: "Dave"}
	data, err := json.Marshal(original)
	if err != nil {
		t.Fatalf("marshal: %v", err)
	}
	var got EmailPayload
	if err := json.Unmarshal(data, &got); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if got.To != original.To || got.FirstName != original.FirstName {
		t.Errorf("roundtrip mismatch: got %+v, want %+v", got, original)
	}
}

// --- Task type constant tests ---

func TestTaskTypeConstants_Unique(t *testing.T) {
	types := []string{
		TypeWelcomeEmail,
		TypeVerificationEmail,
		TypePasswordResetEmail,
		TypePasswordChangedEmail,
	}
	seen := make(map[string]bool)
	for _, typ := range types {
		if seen[typ] {
			t.Errorf("duplicate task type: %q", typ)
		}
		seen[typ] = true
	}
}

func TestTaskTypeConstants_EmailPrefix(t *testing.T) {
	types := []string{
		TypeWelcomeEmail,
		TypeVerificationEmail,
		TypePasswordResetEmail,
		TypePasswordChangedEmail,
	}
	for _, typ := range types {
		if len(typ) < 6 || typ[:6] != "email:" {
			t.Errorf("task type %q does not have 'email:' prefix", typ)
		}
	}
}
