package elinps

import (
	"html/template"
	"path/filepath"
	"strings"
)

func mustParseTemplates() *template.Template {
	// Local filesystem parsing (keeps the repo simple).
	// If you want a single-binary deploy, we can switch to go:embed by moving
	// templates into internal/elinps and embedding without "..".
	funcs := template.FuncMap{
		"seq": func(start, end int) []int {
			if end < start {
				return []int{}
			}
			out := make([]int, 0, end-start+1)
			for i := start; i <= end; i++ {
				out = append(out, i)
			}
			return out
		},
		"noteEq": func(ptr *int, v int) bool {
			return ptr != nil && *ptr == v
		},
		"produtoLabel": func(produto string) string {
			// Best-effort label from normalized produto.
			p := strings.ReplaceAll(produto, "_", " ")
			parts := strings.Fields(p)
			for i := range parts {
				if len(parts[i]) == 0 {
					continue
				}
				parts[i] = strings.ToUpper(parts[i][:1]) + parts[i][1:]
			}
			return strings.Join(parts, " ")
		},
	}

	pattern := filepath.ToSlash("web/templates/*.html")
	return template.Must(template.New("").Funcs(funcs).ParseGlob(pattern))
}
