package infra

import (
	"strings"
	"testing"
)

func TestEmailTemplateRender(t *testing.T) {
	t.Run("renderiza template simple con datos", func(t *testing.T) {
		got, err := EmailTemplateRender("Hola {{.Name}}", map[string]any{"Name": "Alice"})
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		if got != "Hola Alice" {
			t.Errorf("got %q, want %q", got, "Hola Alice")
		}
	})

	t.Run("sustituye multiples variables", func(t *testing.T) {
		tmpl := "Pedido {{.OrderID}} para {{.Customer}} listo."
		got, err := EmailTemplateRender(tmpl, map[string]any{"OrderID": "ORD-42", "Customer": "Bob"})
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		if !strings.Contains(got, "ORD-42") || !strings.Contains(got, "Bob") {
			t.Errorf("got %q, expected OrderID and Customer", got)
		}
	})

	t.Run("error en template invalido", func(t *testing.T) {
		_, err := EmailTemplateRender("{{.Unclosed", nil)
		if err == nil {
			t.Error("expected error for invalid template, got nil")
		}
	})

	t.Run("template vacio retorna string vacio", func(t *testing.T) {
		got, err := EmailTemplateRender("", nil)
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		if got != "" {
			t.Errorf("got %q, want empty string", got)
		}
	})
}
