use std::fs;
use std::path::Path;
use chrono::{DateTime, Local, NaiveDateTime, TimeZone};
use crate::config;

pub fn get_last_update_time() -> Option<DateTime<Local>> {
    let content = fs::read_to_string(config::APT_HISTORY_LOG).ok()?;

    content.lines()
        .filter(|line| line.starts_with("Start-Date: "))
        .last()
        .and_then(|line| {
            let date_str = line.replace("Start-Date: ", "").trim().to_string();
            let date_str = date_str.replace("  ", " ");

            NaiveDateTime::parse_from_str(&date_str, "%Y-%m-%d %H:%M:%S")
                .ok()
                .and_then(|naive| Local.from_local_datetime(&naive).single())
        })
}

pub fn is_live_environment() -> bool {
    if let Ok(cmdline) = fs::read_to_string("/proc/cmdline") {
        if cmdline.contains("boot=live") {
            return true;
        }
    }
    Path::new("/run/live/rootfs/filesystem.squashfs").exists()
}
