// AUTO-GENERATED by mizan — do not edit

use serde::{Deserialize, Serialize};
use serde_json::Value;

use mizan_rust::{MizanClient, MizanError};

{% if !type_imports.is_empty() -%}
use crate::types::{ {{- type_imports|join(", ") -}} };

{% endif -%}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct {{ pascal }}ContextData {
{% for field in data_fields -%}
{% if field.has_rename %}    #[serde(rename = "{{ field.raw_name }}")]
{% endif %}    pub {{ field.ident }}: {{ field.ty }},
{% endfor -%}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct {{ pascal }}ContextParams {
{% for p in params -%}
{% if p.has_rename %}    #[serde(rename = "{{ p.raw_name }}")]
{% endif %}    pub {{ p.ident }}: {{ p.ty }},
{% endfor -%}
}

pub async fn fetch_{{ snake }}_context(
    client: &MizanClient,
    params: &{{ pascal }}ContextParams,
) -> Result<{{ pascal }}ContextData, MizanError> {
    let params_value = serde_json::to_value(params).unwrap_or(Value::Object(Default::default()));
    let raw = client.fetch_context("{{ ctx_name }}", &params_value).await?;
    serde_json::from_value(raw)
        .map_err(|e| MizanError::transport(format!("decode {{ ctx_name }} context: {e}")))
}

