use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistorySeries {
    pub label: String,
    pub points: Vec<DataPoint>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataPoint {
    pub timestamp: String,
    pub value: f64,
}

impl HistorySeries {
    pub fn new(label: impl Into<String>) -> Self {
        Self {
            label: label.into(),
            points: Vec::new(),
        }
    }

    pub fn push(&mut self, timestamp: impl Into<String>, value: f64) {
        self.points.push(DataPoint {
            timestamp: timestamp.into(),
            value,
        });
    }

    pub fn avg(&self) -> f64 {
        if self.points.is_empty() {
            return 0.0;
        }
        self.points.iter().map(|p| p.value).sum::<f64>() / self.points.len() as f64
    }

    pub fn peak(&self) -> f64 {
        self.points.iter().map(|p| p.value).fold(0.0_f64, f64::max)
    }
}
