use std::time::Instant;

#[cfg(target_os = "freebsd")]
mod freebsd;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;

#[derive(Debug, Clone)]
pub struct MemorySample {
    pub timestamp: Instant,
    pub rss_bytes: u64,
    pub peak_rss_bytes: u64,
    pub virtual_bytes: u64,
    pub swap_bytes: u64,
}

#[derive(Debug, thiserror::Error)]
pub enum MemoryError {
    #[error("Process not found (PID {0})")]
    ProcessNotFound(u32),
    #[error("Permission denied accessing PID {0}")]
    PermissionDenied(u32),
    #[error("Platform not supported")]
    Unsupported,
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),
}

pub fn sample_process_memory(pid: u32) -> Result<MemorySample, MemoryError> {
    #[cfg(target_os = "linux")]
    return linux::sample_memory(pid);

    #[cfg(target_os = "macos")]
    return macos::sample_memory(pid);

    #[cfg(target_os = "freebsd")]
    return freebsd::sample_memory(pid);

    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "freebsd")))]
    Err(MemoryError::Unsupported)
}
