use std::sync::Arc;
use axum::extract::{Path, State};
use axum::Json;
use serde::Deserialize;
use crate::error::AppError;
use crate::server::AppState;
pub(crate) async fn handle_spaces_list(
state: State<Arc<AppState>>,
) -> Result<Json<serde_json::Value>, AppError> {
let spaces = state.kernel.spaces.list_spaces();
Ok(Json(serde_json::json!({
"items": spaces,
"total": spaces.len(),
})))
}
pub(crate) async fn handle_space_current(
state: State<Arc<AppState>>,
) -> Result<Json<serde_json::Value>, AppError> {
match state.kernel.spaces.current_space() {
Some(space) => Ok(Json(serde_json::to_value(&space).unwrap_or_default())),
None => Ok(Json(serde_json::json!(null))),
}
}
pub(crate) async fn handle_space_get(
state: State<Arc<AppState>>,
Path(id): Path<String>,
) -> Result<Json<serde_json::Value>, AppError> {
match state.kernel.spaces.get_space(&id).await {
Some(space) => Ok(Json(serde_json::to_value(&space).unwrap_or_default())),
None => Err(AppError::NotFound(format!("Space {id} not found"))),
}
}
pub(crate) async fn handle_space_activate(
state: State<Arc<AppState>>,
Path(id): Path<String>,
) -> Result<Json<serde_json::Value>, AppError> {
state
.kernel
.activate_space(&id)
.await
.map_err(|e| AppError::Internal(e.to_string()))?;
Ok(Json(serde_json::json!({ "ok": true, "space_id": id })))
}
pub(crate) async fn handle_space_archive(
state: State<Arc<AppState>>,
Path(id): Path<String>,
) -> Result<Json<serde_json::Value>, AppError> {
state
.kernel
.spaces
.archive(&id)
.await
.map_err(|e| AppError::Internal(e.to_string()))?;
Ok(Json(serde_json::json!({ "ok": true, "space_id": id })))
}
pub(crate) async fn handle_space_restore(
state: State<Arc<AppState>>,
Path(id): Path<String>,
) -> Result<Json<serde_json::Value>, AppError> {
state
.kernel
.spaces
.restore(&id)
.await
.map_err(|e| AppError::Internal(e.to_string()))?;
Ok(Json(serde_json::json!({ "ok": true, "space_id": id })))
}
#[derive(Debug, Deserialize)]
pub(crate) struct MergeRequest {
pub survivor_id: String,
pub absorbed_id: String,
}
pub(crate) async fn handle_space_merge(
state: State<Arc<AppState>>,
Json(body): Json<MergeRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
state
.kernel
.spaces
.merge(&body.survivor_id, &body.absorbed_id)
.await
.map_err(|e| AppError::Internal(e.to_string()))?;
Ok(Json(serde_json::json!({
"ok": true,
"survivor_id": body.survivor_id,
"absorbed_id": body.absorbed_id,
})))
}
pub(crate) async fn handle_memory_flow(
state: State<Arc<AppState>>,
) -> Result<Json<serde_json::Value>, AppError> {
let flows = state.kernel.spaces.memory_flow();
Ok(Json(serde_json::json!({
"items": flows,
"total": flows.len(),
})))
}
pub(crate) async fn handle_memory_flow_for(
state: State<Arc<AppState>>,
Path(id): Path<String>,
) -> Result<Json<serde_json::Value>, AppError> {
match state.kernel.spaces.memory_flow_for(&id) {
Some(flows) => Ok(Json(serde_json::json!({
"items": flows,
"total": flows.len(),
}))),
None => Err(AppError::NotFound(format!(
"Space {id} not found for memory flow"
))),
}
}