package postgres

import (
	"context"
	"time"

	"switchboard-core/database"
)

// ── HealthStore ─────────────────────────────

type HealthStore struct{}

func NewHealthStore() *HealthStore { return &HealthStore{} }

// Prune deletes stale kernel data older than the given time.
// Cleans: expired ws_tickets, old rate_limit_counters, stale presence.
func (s *HealthStore) Prune(ctx context.Context, before time.Time) (int64, error) {
	var total int64

	res, _ := database.DB.ExecContext(ctx,
		`DELETE FROM ws_tickets WHERE expires_at < $1`, before)
	if res != nil {
		n, _ := res.RowsAffected()
		total += n
	}

	res, _ = database.DB.ExecContext(ctx,
		`DELETE FROM rate_limit_counters WHERE window_start < $1`, before)
	if res != nil {
		n, _ := res.RowsAffected()
		total += n
	}

	res, _ = database.DB.ExecContext(ctx,
		`DELETE FROM user_presence WHERE last_seen < $1 AND status = 'offline'`, before)
	if res != nil {
		n, _ := res.RowsAffected()
		total += n
	}

	return total, nil
}
