package template

import (
	"fmt"

	"gateway/internal/model/notification/domain/enum"
)

// Registry maps NotifyKind and locale to template specs.
type Registry map[enum.NotifyKind]map[string]Spec

// Lookup returns the spec for kind and locale, with locale fallback.
func (r Registry) Lookup(kind enum.NotifyKind, locale string, fallbacks ...string) (Spec, error) {
	byLocale, ok := r[kind]
	if !ok {
		return Spec{}, fmt.Errorf("template: unknown kind %q", kind)
	}

	try := append([]string{locale}, fallbacks...)
	for _, loc := range try {
		if loc == "" {
			continue
		}
		if spec, ok := byLocale[loc]; ok {
			return spec, nil
		}
	}
	return Spec{}, fmt.Errorf("template: no locale for kind %q (locale=%q)", kind, locale)
}
