package handler

import "net/http"

func (h *Handler) handleGetLastLog(w http.ResponseWriter, r *http.Request) {
	id, err := pathID(r, "id")
	if err != nil {
		writeError(w, http.StatusBadRequest, "Ungültige ID")
		return
	}

	lastLog, err := h.store.GetLastLog(id)
	if err != nil {
		writeError(w, http.StatusInternalServerError, "Fehler beim Laden des letzten Logs")
		return
	}
	if lastLog == nil {
		writeError(w, http.StatusNotFound, "Noch kein Log für diese Übung")
		return
	}
	writeJSON(w, http.StatusOK, lastLog)
}

func (h *Handler) handleGetExerciseHistory(w http.ResponseWriter, r *http.Request) {
	id, err := pathID(r, "id")
	if err != nil {
		writeError(w, http.StatusBadRequest, "Ungültige ID")
		return
	}
	limit := queryInt(r, "limit", 30)

	logs, err := h.store.GetExerciseHistory(id, limit)
	if err != nil {
		writeError(w, http.StatusInternalServerError, "Fehler beim Laden der Übungshistorie")
		return
	}
	writeJSON(w, http.StatusOK, logs)
}

func (h *Handler) handleGetStatsOverview(w http.ResponseWriter, r *http.Request) {
	overview, err := h.store.GetStatsOverview()
	if err != nil {
		writeError(w, http.StatusInternalServerError, "Fehler beim Laden der Statistiken")
		return
	}
	writeJSON(w, http.StatusOK, overview)
}
