package handlers

import (
	"html/template"
	"net/http"

	"decor-by-hannahs/internal/db"
)

func CatalogHandler(q *db.Queries, tmpl *template.Template) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		svcs, err := q.ListServices(r.Context())
		if err != nil {
			http.Error(w, "unable to load services", http.StatusInternalServerError)
			return
		}
		type data struct {
			Services   []db.Service
			ActivePage string
		}
		if err := tmpl.ExecuteTemplate(w, "catalog.tmpl", data{Services: svcs, ActivePage: "catalog"}); err != nil {
			http.Error(w, "Failed to render page", http.StatusInternalServerError)
		}
	}
}
