use super::types::{CompactSummary, MessageSummary};

pub fn messages_to_text<F>(
    messages: &[models::rooms::room_message::Model],
    sender_mapper: F,
) -> String
where
    F: Fn(&models::rooms::room_message::Model) -> String,
{
    messages
        .iter()
        .map(|m| {
            let sender = sender_mapper(m);
            format!("[{}] {}: {}", m.send_at, sender, m.content)
        })
        .collect::<Vec<_>>()
        .join("\n")
}

pub fn retained_as_text(retained: &[MessageSummary]) -> String {
    retained
        .iter()
        .map(|m| format!("[{}] {}: {}", m.send_at, m.sender_name, m.content))
        .collect::<Vec<_>>()
        .join("\n")
}

pub fn summary_content(summary: &CompactSummary) -> String {
    if summary.summary.is_empty() {
        format!(
            "## Recent conversation ({} messages)\n\n{}",
            summary.retained.len(),
            retained_as_text(&summary.retained)
        )
    } else {
        format!(
            "## Earlier conversation ({} messages summarised)\n{}\n\n\
             ## Most recent {} messages\n\n{}",
            summary.messages_compressed,
            summary.summary,
            summary.retained.len(),
            retained_as_text(&summary.retained)
        )
    }
}
