package db

import (
	"context"
	"fmt"
	"time"
)

// PruneEmpyrionEvents deletes empyrion_events rows older than keep.
// empyrion_events is append-only (one row per bridge Event_* package) and
// grows without bound on a busy server; the history/activity views only
// ever page over recent windows. Returns the number of rows deleted.
//
// empyrion_known_players is NOT pruned here: it is already compact — one
// upserted row per (instance_id, player_id) carrying login_count and
// first/last_seen aggregates the players view depends on.
func (db *DB) PruneEmpyrionEvents(ctx context.Context, keep time.Duration) (int64, error) {
	tag, err := db.pool.Exec(ctx,
		`DELETE FROM empyrion_events WHERE ts < NOW() - $1::interval`,
		fmt.Sprintf("%d seconds", int64(keep.Seconds())))
	if err != nil {
		return 0, fmt.Errorf("prune empyrion_events: %w", err)
	}
	return tag.RowsAffected(), nil
}
