package handler

import (
	"encoding/csv"
	"net/http"
	"strconv"
	"time"
)

// handleExport gibt alle Trainingsdaten eines Nutzers als CSV zurück.
func (h *Handler) handleExport(w http.ResponseWriter, r *http.Request) {
	uid, err := userID(r)
	if err != nil {
		writeError(w, http.StatusBadRequest, err.Error())
		return
	}

	rows, err := h.store.ExportLogs(uid)
	if err != nil {
		writeError(w, http.StatusInternalServerError, "Export fehlgeschlagen")
		return
	}

	filename := "training-export-" + time.Now().Format("2006-01-02") + ".csv"
	w.Header().Set("Content-Type", "text/csv; charset=utf-8")
	w.Header().Set("Content-Disposition", "attachment; filename=\""+filename+"\"")

	cw := csv.NewWriter(w)
	cw.Write([]string{"datum", "uebung", "satz", "gewicht_kg", "wiederholungen", "notiz"})

	for _, row := range rows {
		cw.Write([]string{
			row.SessionStarted.Format("2006-01-02 15:04"),
			row.ExerciseName,
			strconv.Itoa(row.SetNumber),
			strconv.FormatFloat(row.WeightKg, 'f', 2, 64),
			strconv.Itoa(row.Reps),
			row.Note,
		})
	}
	cw.Flush()
}
