use crate::core::client::Client;
use crate::shared::{ApiResponse, Error, Result};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;
use tokio_util::sync::CancellationToken;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BugReportResult {
pub feedback_id: String,
pub detail_url: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BugView {
pub id: String,
pub description: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub platform: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub terminal: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
#[serde(rename = "messageCount")]
pub message_count: i64,
#[serde(rename = "hasErrors")]
pub has_errors: bool,
pub status: String,
#[serde(
rename = "clientDatetime",
default,
skip_serializing_if = "Option::is_none"
)]
pub client_datetime: Option<String>,
#[serde(rename = "createdAt")]
pub created_at: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub errors: Option<Vec<Value>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub transcript: Option<Vec<Value>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extras: Option<HashMap<String, Value>>,
}
impl Client {
pub async fn submit_bug_report(
&self,
report_data: &Value,
signal: Option<CancellationToken>,
) -> Result<BugReportResult> {
if report_data.is_null() {
return Err(Error::other("acosmi: reportData required"));
}
let content_str = serde_json::to_string(report_data)
.map_err(|e| Error::other(format!("acosmi: marshal reportData: {e}")))?;
let body = json!({ "content": content_str }).to_string();
let (env, _) = self
.do_json_full::<ApiResponse<BugReportResult>>(
reqwest::Method::POST,
"/crabcode_cli_feedback",
Some(&body),
signal,
)
.await?;
let env = env.ok_or_else(|| Error::other("/crabcode_cli_feedback: empty response body"))?;
if let Some(err) = env.business_error() {
return Err(err);
}
Ok(env.data)
}
pub async fn get_bug_report(
&self,
bug_id: &str,
signal: Option<CancellationToken>,
) -> Result<BugView> {
let trimmed = bug_id.trim();
if trimmed.is_empty() {
return Err(Error::other("acosmi: bugID required"));
}
let env: ApiResponse<BugView> = self
.do_public_json_full(&format!("/crabcode/bug/{trimmed}"), signal)
.await?;
if let Some(err) = env.business_error() {
return Err(err);
}
Ok(env.data)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bug_report_result_wire_snake_case() {
let r: BugReportResult = serde_json::from_str(
r#"{"feedback_id":"uuid-1","detail_url":"https://x/chat/crabcode/bug/uuid-1"}"#,
)
.unwrap();
assert_eq!(r.feedback_id, "uuid-1");
assert_eq!(r.detail_url, "https://x/chat/crabcode/bug/uuid-1");
let back = serde_json::to_value(&r).unwrap();
assert_eq!(back["feedback_id"], "uuid-1");
assert_eq!(back["detail_url"], "https://x/chat/crabcode/bug/uuid-1");
}
#[test]
fn bug_view_camel_case_wire() {
let v: BugView = serde_json::from_str(
r#"{"id":"b1","description":"d","messageCount":3,"hasErrors":true,"status":"open","createdAt":"2026-05-01T00:00:00","extras":{"k":"v"}}"#,
)
.unwrap();
assert_eq!(v.id, "b1");
assert_eq!(v.message_count, 3);
assert!(v.has_errors);
assert_eq!(v.created_at, "2026-05-01T00:00:00");
assert!(v.extras.is_some());
}
}