use night_fury_daemon_core::protocol::Response;

pub fn ok_value(id: &str, value: serde_json::Value) -> Response {
    Response::ok(id, value)
}

/// Build an error `Response` from a typed `TailFinError`, attaching its
/// machine-readable `error_kind` to the wire when one is available. Falls back
/// to a kindless `Response::err` only if `error_kind()` is `None`.
pub fn err_for(id: &str, e: &tail_fin_common::TailFinError) -> Response {
    match e.error_kind() {
        Some(kind) => Response::err_kind(id, e.to_string(), kind),
        None => Response::err(id, e.to_string()),
    }
}

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

    #[test]
    fn err_for_carries_kind_when_present() {
        let resp = err_for("req-1", &TailFinError::Timeout("boom".into()));
        assert!(!resp.ok);
        assert_eq!(resp.error.as_deref(), Some("timeout: boom"));
        assert_eq!(resp.error_kind.as_deref(), Some("timeout"));
    }

    #[test]
    fn err_for_api_variant_kind_is_api() {
        let resp = err_for("req-2", &TailFinError::Api("nope".into()));
        assert!(!resp.ok);
        assert_eq!(resp.error_kind.as_deref(), Some("api"));
    }
}
