package webhooks

import "testing"

func TestInterpolate(t *testing.T) {
	ctx := &MessageContext{
		SenderName:    "Alice Example",
		SenderEmail:   "alice@example.com",
		Subject:       "Hello World",
		BodyText:      "Plain body",
		BodyHTML:      "<p>HTML body</p>",
		Date:          "2026-05-22T10:00:00Z",
		Recipients:    "bob@example.com",
		HasAttachment: true,
		MessageID:     "msg-123",
	}

	template := `{
  "from": "$sender.name <$sender.email>",
  "subject": "$subject",
  "text": "$body.textContent",
  "html": "$body.htmlContent",
  "date": "$date",
  "to": "$recipients.to",
  "id": "$message_id"
}`

	want := `{
  "from": "Alice Example <alice@example.com>",
  "subject": "Hello World",
  "text": "Plain body",
  "html": "<p>HTML body</p>",
  "date": "2026-05-22T10:00:00Z",
  "to": "bob@example.com",
  "id": "msg-123"
}`

	if got := interpolate(template, ctx); got != want {
		t.Fatalf("interpolate() =\n%s\nwant\n%s", got, want)
	}
}

func TestInterpolate_unknownVariablesUnchanged(t *testing.T) {
	ctx := &MessageContext{Subject: "Test"}
	got := interpolate("Subject: $subject, Extra: $unknown.var", ctx)
	want := "Subject: Test, Extra: $unknown.var"
	if got != want {
		t.Fatalf("interpolate() = %q, want %q", got, want)
	}
}

func TestInterpolate_emptyContext(t *testing.T) {
	got := interpolate("$sender.email $subject", &MessageContext{})
	want := " "
	if got != want {
		t.Fatalf("interpolate() = %q, want %q", got, want)
	}
}
