package datascience

import "sort"

// DurationStatsFrom computes descriptive statistics for a slice of durations in milliseconds.
// It sorts a local copy so the original slice is not mutated.
// Returns a zero-value DurationStats when the input is empty.
func DurationStatsFrom(durations []int64) DurationStats {
	n := len(durations)
	if n == 0 {
		return DurationStats{}
	}
	cp := make([]int64, n)
	copy(cp, durations)
	sort.Slice(cp, func(i, j int) bool { return cp[i] < cp[j] })
	var sum int64
	for _, d := range cp {
		sum += d
	}
	return DurationStats{
		N:     n,
		AvgMs: sum / int64(n),
		P50Ms: Percentile(cp, 0.5),
		P90Ms: Percentile(cp, 0.9),
		P99Ms: Percentile(cp, 0.99),
	}
}
