package handlers

import (
	"encoding/json"
	"net/http"

	"mgit.msbls.de/m/paliad/internal/calc"
)

func handleKostenrechnerPage(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "dist/kostenrechner.html")
}

func handleKostenrechnerAPI(w http.ResponseWriter, r *http.Request) {
	var req calc.CostRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Ungültige Anfrage"})
		return
	}

	if req.Streitwert <= 0 {
		writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Streitwert muss positiv sein"})
		return
	}

	resp, err := calc.Calculate(req)
	if err != nil {
		writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
		return
	}

	writeJSON(w, http.StatusOK, resp)
}
