use std::sync::LazyLock;
use regex::Regex;

static P_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)</?p[^>]*>").unwrap());
static BR_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)<br\s*/?>").unwrap());
static SPAN_HEADING_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)</?(span|h[1-6])[^>]*>").unwrap());
static DOUBLE_NEWLINE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\r?\n\s*\n").unwrap());
static SPACES_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[ \t]+").unwrap());


/// Returns the current UNIX timestamp in seconds.
/// Equivalent to TS `System.timeStampInSeconds()`.
pub fn timestamp_in_seconds() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|duration| duration.as_secs() as i64)
        .unwrap_or(0)
}

/// Creates a new UUID v4 string, or reuses an existing ID if provided.
/// Equivalent to TS `System.createUniqueId()`.
pub fn create_unique_id(existing_id: Option<&str>) -> String {
    match existing_id {
        Some(id) => id.to_string(),
        None => uuid::Uuid::new_v4().to_string(),
    }
}


/// Converts HTML content to plain text by stripping tags and decoding entities.
/// Equivalent to TS `System.htmlToText()`.
pub fn html_to_text(html_node: &str) -> String {
    let mut text: String = P_REGEX.replace_all(html_node, "\n").to_string();
    text = BR_REGEX.replace_all(&text, "\n").to_string();
    text = SPAN_HEADING_REGEX.replace_all(&text, "").to_string();
    text = text.replace("&apos;", "'");
    text = text.replace("&quot;", "\"");
    text = text.replace("&amp;", "&");
    text = text.replace("&lt;", "<");
    text = text.replace("&gt;", ">");
    text = text.replace("&#39;", "'");
    text = DOUBLE_NEWLINE_REGEX.replace_all(&text, "\n").to_string();
    text = SPACES_REGEX.replace_all(&text, " ").to_string();
    text.trim().to_string()
}
