package web

import (
	"log/slog"
	"net/http"

	"github.com/spf13/viper"
)

func IndexWebHandler(w http.ResponseWriter, r *http.Request) {
	component := Index()
	err := component.Render(r.Context(), w)
	if err != nil {
		slog.Error("Error rendering in IndexWebHandler", "error", err)
		http.Error(w, err.Error(), http.StatusBadRequest)
	}
}

// IndexUploadHandler handles the upload of a new file
func IndexUploadHandler(w http.ResponseWriter, r *http.Request) {
  if r.Method != http.MethodPost {
		http.Error(w, "Only POST allowed", http.StatusBadRequest)
    return
  }

	maxUploadSize := viper.GetInt64("web.maxfilesizemb") * 1024 * 1024
	r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize)
	defer r.Body.Close()
  file, fileHeader, err := r.FormFile("file")
	if err != nil {
		slog.Error("Error parsing form in IndexUploadHandler", "error", err)
		http.Error(w, err.Error(), http.StatusBadRequest)
	}

}
