package web

import (
	"embed"
	"fmt"
	"io/fs"
	"net/http"
	"text/template"

	"github.com/olivere/vite"
)

type PageData struct {
	Title    string
	MsgType  string
	Message  string
	Redirect string
	Vite     *vite.Fragment
}

//go:embed templates
var Templates embed.FS

//go:embed all:frontend/dist
var StaticAssets embed.FS

func DistFS() fs.FS {
	efs, err := fs.Sub(StaticAssets, "frontend/dist")
	if err != nil {
		panic(fmt.Sprintf("unable to serve frontend: %v", err))
	}
	return efs
}

func RenderTemplate(w http.ResponseWriter, name string, data PageData) error {
	tpl := template.Must(template.ParseFS(Templates, "templates/"+name+".html", "templates/base.html"))
	return tpl.ExecuteTemplate(w, "base", data)
}
