use serde_json::Value;
use reqwest::Client;

/// ส่ง JSON POST ไปยัง PHP แล้วรับ JSON กลับมา
pub async fn ajax_post(url: &str, data: &Value) -> Result<Value, String> {
    let client = Client::new();

    let res = client
        .post(url)
        .json(data)
        .send()
        .await
        .map_err(|e| format!("HTTP Error: {}", e))?;

    let status = res.status().as_u16();

    if status != 200 {
        return Err(format!("Server returned HTTP {}", status));
    }

    let json = res
        .json::<Value>()
        .await
        .map_err(|e| format!("JSON Parse Error: {}", e))?;

    Ok(json)
}
