//! [POST /_synapse/admin/v1/background_updates/enabled](https://github.com/element-hq/synapse/blob/develop/docs/usage/administration/admin_api/background_updates.md#enabled)

use ruma::{
    api::{auth_scheme::AccessToken, request, response},
    metadata,
};
use serde::{Deserialize, Serialize};

metadata! {
    method: POST,
    rate_limited: false,
    authentication: AccessToken,
    path: "/_synapse/admin/v1/background_updates/enabled",
}

#[request]
pub struct Request {
    /// Sets whether the background updates are enabled.
    pub enabled: bool,
}

#[response]
#[derive(Serialize, Deserialize, PartialEq)]
pub struct Response {
    /// Whether the background updates are enabled or disabled.
    pub enabled: bool,
}

impl Request {
    /// Creates a `Request` with the given `enabled` value.
    pub fn new(enabled: bool) -> Self {
        Self { enabled }
    }
}

impl Response {
    /// Creates a `Response` with the given `enabled` value.
    pub fn new(enabled: bool) -> Self {
        Self { enabled }
    }
}

#[test]
fn test_enabled_background_updates() {
    let enabled = true;

    // Check create request
    let request = Request::new(enabled);
    assert!(request.enabled);

    // Check create response
    let response = Response::new(enabled);
    assert!(request.enabled);

    // Serialize
    let serialized = serde_json::to_string(&response).expect("Failed to serialize");
    assert_eq!(serialized, "{\"enabled\":true}");

    // Deserialize
    let deserialized: Response = serde_json::from_str(&serialized).expect("Failed to deserialize");
    assert_eq!(deserialized, response);
    assert!(deserialized.enabled);
}
