package handlers

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

	"git.espin.casa/albert/cml04-falcon-printer/storage"
	"git.espin.casa/albert/cml04-falcon-printer/types"
	"github.com/julienschmidt/httprouter"
)

type PageView struct {
	ProductionOrders []*types.ProductionOrder
	CustomerOrders   []*types.CustomerOrder
}

func IndexHandler(storage storage.Storager) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		// get production orders
		pos, err := storage.ListProductionOrders(r.Context(), 40)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		// create view
		view := &PageView{
			ProductionOrders: pos,
			CustomerOrders:   nil,
		}
		t, _ := template.ParseFiles("templates/base.html", "templates/index.html")
		if err := t.Execute(w, view); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}
}
