//! `GET /_matrix/client/versions` ([spec])
//!
//! Get the versions of the client-server API supported by this homeserver.
//!
//! [spec]: https://spec.matrix.org/v1.18/client-server-api/#get_matrixclientversions

use std::collections::BTreeMap;

use ruma_common::{
    api::{SupportedVersions, auth_scheme::AccessTokenOptional, request, response},
    metadata,
};
#[cfg(feature = "unstable-msc4383")]
use serde::{Deserialize, Serialize};

metadata! {
    method: GET,
    rate_limited: false,
    authentication: AccessTokenOptional,
    path: "/_matrix/client/versions",
}

/// Request type for the `api_versions` endpoint.
#[request]
#[derive(Default)]
pub struct Request {}

/// Response type for the `api_versions` endpoint.
#[response]
pub struct Response {
    /// A list of Matrix client API protocol versions supported by the homeserver.
    pub versions: Vec<String>,

    /// Experimental features supported by the server.
    ///
    /// Servers can enable some unstable features only for some users, so this
    /// list might differ when an access token is provided.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub unstable_features: BTreeMap<String, bool>,

    /// MSC4383: information about the homeserver implementation, with parity to
    /// the eponymous object returned by `GET /_matrix/federation/v1/version`.
    #[cfg(feature = "unstable-msc4383")]
    #[serde(
        rename = "net.zemos.msc4383.server",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub server: Option<Server>,
}

/// Arbitrary values that identify this implementation. Mirrors
/// `ruma_federation_api::discovery::get_server_version::v1::Server`.
#[cfg(feature = "unstable-msc4383")]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct Server {
    /// Arbitrary name that identifies this implementation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// Version of this implementation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,

    /// Sourcecode version.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub commit: Option<String>,

    /// Compiler version.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub compiler: Option<String>,

    /// System version.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kernel: Option<String>,

    /// Hardware architecture.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub arch: Option<String>,
}

#[cfg(feature = "unstable-msc4383")]
impl Server {
    /// Creates an empty `Server`.
    pub fn new() -> Self {
        Default::default()
    }
}

impl Request {
    /// Creates an empty `Request`.
    pub fn new() -> Self {
        Self {}
    }
}

impl Response {
    /// Creates a new `Response` with the given `versions`.
    pub fn new(versions: Vec<String>) -> Self {
        Self {
            versions,
            unstable_features: BTreeMap::new(),
            #[cfg(feature = "unstable-msc4383")]
            server: None,
        }
    }

    /// Convert this `Response` into a [`SupportedVersions`] that can be used with
    /// `OutgoingRequest::try_into_http_request()`.
    ///
    /// Matrix versions that can't be parsed to a `MatrixVersion`, and features with the boolean
    /// value set to `false` are discarded.
    pub fn as_supported_versions(&self) -> SupportedVersions {
        SupportedVersions::from_parts(&self.versions, &self.unstable_features)
    }
}
