// Package registry provides functionality for managing and accessing embedded file systems and directories.
package registry

import (
	"embed"
	"io"
	"io/fs"
	"path/filepath"
	"strings"
)

//go:embed templates/*.marka
var templates embed.FS

func GetTemplate(name string) (string, error) {
	templateFile, err := templates.Open("templates/" + name + ".marka")
	if err != nil {
		return "", err
	}
	defer templateFile.Close()

	templateBytes, err := io.ReadAll(templateFile)
	if err != nil {
		return "", err
	}

	return string(templateBytes), nil
}

func ListTemplates() ([]string, error) {
	var templateNames []string
	err := fs.WalkDir(templates, ".", func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if !d.IsDir() && strings.HasSuffix(path, ".marka") {
			templateNames = append(templateNames, strings.TrimSuffix(filepath.Base(path), ".marka"))
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	return templateNames, nil
}
