//! Proxies for the daemon's `coder.projects.*` namespace (CAR-managed
//! projects). Thin [`DaemonClient`] calls — projects live in the daemon.

use serde_json::{json, Value};

use crate::proxy::DaemonClient;

fn to_string(v: Value) -> Result<String, String> {
    serde_json::to_string(&v).map_err(|e| e.to_string())
}

/// `coder.projects.create` — create/load a managed project. `kind` is
/// `app | agent` (None = app).
pub async fn create(
    client: &DaemonClient,
    name: &str,
    kind: Option<&str>,
) -> Result<String, String> {
    let mut params = json!({ "name": name });
    if let Some(kind) = kind {
        params["kind"] = json!(kind);
    }
    client
        .call("coder.projects.create", params)
        .await
        .and_then(to_string)
}

/// `coder.projects.list` — all managed projects, newest first.
pub async fn list(client: &DaemonClient) -> Result<String, String> {
    client
        .call("coder.projects.list", json!({}))
        .await
        .and_then(to_string)
}

/// `coder.projects.get` — one project's metadata by slug.
pub async fn get(client: &DaemonClient, slug: &str) -> Result<String, String> {
    client
        .call("coder.projects.get", json!({ "slug": slug }))
        .await
        .and_then(to_string)
}
