//! Hosting Service Usage Metering

use serde::{Deserialize, Serialize};

/// Hosting service usage metrics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct HostingUsage {
    /// Bandwidth consumed in bytes
    pub bandwidth_bytes: u64,
    /// Number of custom domains active
    pub custom_domains: u32,
    /// Number of HTTP requests
    pub requests: u64,
    /// Number of unique visitors
    pub unique_visitors: u64,
}

impl HostingUsage {
    /// Create new hosting usage
    pub fn new() -> Self {
        Self::default()
    }

    /// Add bandwidth
    pub fn with_bandwidth(mut self, bytes: u64) -> Self {
        self.bandwidth_bytes = bytes;
        self
    }

    /// Add custom domains
    pub fn with_domains(mut self, count: u32) -> Self {
        self.custom_domains = count;
        self
    }

    /// Add requests
    pub fn with_requests(mut self, count: u64) -> Self {
        self.requests = count;
        self
    }

    /// Add unique visitors
    pub fn with_visitors(mut self, count: u64) -> Self {
        self.unique_visitors = count;
        self
    }

    /// Convert bandwidth to GB
    pub fn bandwidth_gb(&self) -> f64 {
        self.bandwidth_bytes as f64 / 1_073_741_824.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_hosting_usage() {
        let usage = HostingUsage::new()
            .with_bandwidth(10_737_418_240) // 10 GB
            .with_domains(3)
            .with_requests(100_000)
            .with_visitors(5_000);

        assert_eq!(usage.bandwidth_gb(), 10.0);
        assert_eq!(usage.custom_domains, 3);
        assert_eq!(usage.requests, 100_000);
    }
}
