use crate::agent::AgentError;

pub(crate) fn enrich_user_content_with_media_refs(
    content: &str,
    media_refs: &[String],
) -> Result<String, AgentError> {
    if media_refs.is_empty() {
        return Ok(content.to_string());
    }

    let media_refs_json = serde_json::to_string(media_refs)
        .map_err(|err| AgentError::Other(format!("serialize media refs error: {}", err)))?;

    Ok(format!("{content}\n\nmedia_refs_json: {media_refs_json}"))
}

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

    #[test]
    fn test_enrich_user_content_with_media_refs_appends_tagged_json() {
        // 使用临时目录确保跨平台兼容
        let temp_dir = tempfile::tempdir().unwrap();
        let media_a = temp_dir.path().join("a.png");
        let media_b = temp_dir.path().join("b.pdf");
        let media_refs = vec![media_a.display().to_string(), media_b.display().to_string()];

        let enriched = enrich_user_content_with_media_refs("hello", &media_refs).unwrap();

        // 验证 JSON 格式正确
        assert!(enriched.starts_with("hello\n\nmedia_refs_json: "));
        assert!(enriched.contains("a.png"));
        assert!(enriched.contains("b.pdf"));
    }

    #[test]
    fn test_enrich_user_content_with_media_refs_keeps_plain_text_without_media() {
        let enriched = enrich_user_content_with_media_refs("hello", &[]).unwrap();

        assert_eq!(enriched, "hello");
    }
}
