package infra

import (
	"bytes"
	"fmt"
	"text/template"
)

// EmailTemplateRender executes a Go text/template with the given data map
// and returns the rendered string.
// tmplStr is the template source (Go text/template syntax).
// data is a map of string keys to any values available inside the template.
// Returns an error if the template fails to parse or execute.
func EmailTemplateRender(tmplStr string, data map[string]any) (string, error) {
	tmpl, err := template.New("email").Parse(tmplStr)
	if err != nil {
		return "", fmt.Errorf("email_template_render: parse: %w", err)
	}
	var buf bytes.Buffer
	if err := tmpl.Execute(&buf, data); err != nil {
		return "", fmt.Errorf("email_template_render: execute: %w", err)
	}
	return buf.String(), nil
}
