pub mod v3 {
    use serde_json::Value;

    #[deprecated(
        since = "0.31.2",
        note = "Please use `get_multiple_components` in module `cfs::component::mesa::http_clent` instead"
    )]
    pub async fn get_multiple_components(
        shasta_token: &str,
        shasta_base_url: &str,
        shasta_root_cert: &[u8],
        components_ids: Option<&str>,
        status: Option<&str>,
    ) -> Result<Vec<Value>, reqwest::Error> {
        let stupid_limit = 100000;

        let client_builder = reqwest::Client::builder()
            .add_root_certificate(reqwest::Certificate::from_pem(shasta_root_cert)?);

        // Build client
        let client = if let Ok(socks5_env) = std::env::var("SOCKS5") {
            // socks5 proxy
            log::debug!("SOCKS5 enabled");
            let socks5proxy = reqwest::Proxy::all(socks5_env)?;

            // rest client to authenticate
            client_builder.proxy(socks5proxy).build()?
        } else {
            client_builder.build()?
        };

        let api_url = shasta_base_url.to_owned() + "/cfs/v3/components";

        let response_rslt = client
            .get(api_url)
            .query(&[
                ("ids", components_ids),
                ("status", status),
                ("limit", Some(&stupid_limit.to_string())),
            ])
            .bearer_auth(shasta_token)
            .send()
            .await;

        match response_rslt {
            Ok(response) => {
                let components_value = &response.json::<Value>().await?["components"];
                Ok(serde_json::from_value::<Vec<Value>>(components_value.clone()).unwrap())
            }
            Err(error) => Err(error),
        }
    }
}

pub mod v2 {
    use serde_json::Value;

    use crate::cfs::component::shasta::r#struct::Component;

    pub async fn get_single_component(
        shasta_token: &str,
        shasta_base_url: &str,
        shasta_root_cert: &[u8],
        component_id: &str,
    ) -> Result<Value, reqwest::Error> {
        let client_builder = reqwest::Client::builder()
            .add_root_certificate(reqwest::Certificate::from_pem(shasta_root_cert)?);

        // Build client
        let client = if let Ok(socks5_env) = std::env::var("SOCKS5") {
            // socks5 proxy
            log::debug!("SOCKS5 enabled");
            let socks5proxy = reqwest::Proxy::all(socks5_env)?;

            // rest client to authenticate
            client_builder.proxy(socks5proxy).build()?
        } else {
            client_builder.build()?
        };

        let api_url = shasta_base_url.to_owned() + "/cfs/v2/components/" + component_id;

        let response_rslt = client.get(api_url).bearer_auth(shasta_token).send().await;

        match response_rslt {
            Ok(response) => response.json().await,
            Err(error) => Err(error),
        }
    }

    pub async fn patch_component(
        shasta_token: &str,
        shasta_base_url: &str,
        shasta_root_cert: &[u8],
        component: Component,
    ) -> Result<Vec<Value>, reqwest::Error> {
        let client_builder = reqwest::Client::builder()
            .add_root_certificate(reqwest::Certificate::from_pem(shasta_root_cert)?);

        // Build client
        let client = if let Ok(socks5_env) = std::env::var("SOCKS5") {
            // socks5 proxy
            log::debug!("SOCKS5 enabled");
            let socks5proxy = reqwest::Proxy::all(socks5_env)?;

            // rest client to authenticate
            client_builder.proxy(socks5proxy).build()?
        } else {
            client_builder.build()?
        };

        let api_url =
            shasta_base_url.to_owned() + "/cfs/v2/components/" + &component.clone().id.unwrap();

        let response_rslt = client
            .patch(api_url)
            .bearer_auth(shasta_token)
            .json(&component)
            .send()
            .await;

        match response_rslt {
            Ok(response) => response.json::<Vec<Value>>().await,
            Err(error) => Err(error),
        }
    }

    pub async fn patch_component_list(
        shasta_token: &str,
        shasta_base_url: &str,
        shasta_root_cert: &[u8],
        component_list: Vec<Component>,
    ) -> Result<Vec<Value>, reqwest::Error> {
        let client_builder = reqwest::Client::builder()
            .add_root_certificate(reqwest::Certificate::from_pem(shasta_root_cert)?);

        // Build client
        let client = if let Ok(socks5_env) = std::env::var("SOCKS5") {
            // socks5 proxy
            log::debug!("SOCKS5 enabled");
            let socks5proxy = reqwest::Proxy::all(socks5_env)?;

            // rest client to authenticate
            client_builder.proxy(socks5proxy).build()?
        } else {
            client_builder.build()?
        };

        let api_url = shasta_base_url.to_owned() + "/cfs/v2/components";

        let response_rslt = client
            .patch(api_url)
            .bearer_auth(shasta_token)
            .json(&component_list)
            .send()
            .await;

        match response_rslt {
            Ok(response) => response.json::<Vec<Value>>().await,
            Err(error) => Err(error),
        }
    }

    pub async fn delete_single_component(
        shasta_token: &str,
        shasta_base_url: &str,
        shasta_root_cert: &[u8],
        component_id: &str,
    ) -> Result<Value, reqwest::Error> {
        let client_builder = reqwest::Client::builder()
            .add_root_certificate(reqwest::Certificate::from_pem(shasta_root_cert)?);

        // Build client
        let client = if let Ok(socks5_env) = std::env::var("SOCKS5") {
            // socks5 proxy
            log::debug!("SOCKS5 enabled");
            let socks5proxy = reqwest::Proxy::all(socks5_env)?;

            // rest client to authenticate
            client_builder.proxy(socks5proxy).build()?
        } else {
            client_builder.build()?
        };

        let api_url = shasta_base_url.to_owned() + "/cfs/v2/components/" + component_id;

        let response_rslt = client
            .delete(api_url)
            .bearer_auth(shasta_token)
            .send()
            .await;

        match response_rslt {
            Ok(response) => response.json().await,
            Err(error) => Err(error),
        }
    }
}
