package store

import (
	"fmt"
	"time"
)

// ExportRow repräsentiert eine Zeile im Trainingsexport.
type ExportRow struct {
	SessionStarted time.Time
	ExerciseName   string
	SetNumber      int
	WeightKg       float64
	Reps           int
	Note           string
}

// ExportLogs gibt alle Logs abgeschlossener Sessions eines Nutzers zurück (für AI-Export).
func (s *Store) ExportLogs(userID int64) ([]ExportRow, error) {
	rows, err := s.db.Query(`
		SELECT s.started_at, sl.exercise_name, sl.set_number, sl.weight_kg, sl.reps, sl.note
		FROM session_logs sl
		JOIN sessions s ON s.id = sl.session_id
		WHERE s.ended_at IS NOT NULL AND s.user_id = ?
		ORDER BY s.started_at, sl.exercise_name, sl.set_number`, userID)
	if err != nil {
		return nil, fmt.Errorf("Export abfragen: %w", err)
	}
	defer rows.Close()

	var result []ExportRow
	for rows.Next() {
		var row ExportRow
		if err := rows.Scan(&row.SessionStarted, &row.ExerciseName, &row.SetNumber, &row.WeightKg, &row.Reps, &row.Note); err != nil {
			return nil, fmt.Errorf("Export scannen: %w", err)
		}
		result = append(result, row)
	}
	return result, rows.Err()
}
