/*
 * Binance Copy Trading REST API
 *
 * OpenAPI Specification for the Binance Copy Trading REST API
 *
 * The version of the OpenAPI document: 1.0.0
 *
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

#![allow(unused_imports)]
use async_trait::async_trait;
use derive_builder::Builder;
use reqwest;
use rust_decimal::prelude::*;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::collections::BTreeMap;

use crate::common::{
    config::ConfigurationRestApi,
    models::{ParamBuildError, RestApiResponse},
    utils::send_request,
};
use crate::copy_trading::rest_api::models;

const HAS_TIME_UNIT: bool = false;

#[async_trait]
pub trait FutureCopyTradingApi: Send + Sync {
    async fn get_futures_lead_trader_status(
        &self,
        params: GetFuturesLeadTraderStatusParams,
    ) -> anyhow::Result<RestApiResponse<models::GetFuturesLeadTraderStatusResponse>>;
    async fn get_futures_lead_trading_symbol_whitelist(
        &self,
        params: GetFuturesLeadTradingSymbolWhitelistParams,
    ) -> anyhow::Result<RestApiResponse<models::GetFuturesLeadTradingSymbolWhitelistResponse>>;
}

#[derive(Debug, Clone)]
pub struct FutureCopyTradingApiClient {
    configuration: ConfigurationRestApi,
}

impl FutureCopyTradingApiClient {
    pub fn new(configuration: ConfigurationRestApi) -> Self {
        Self { configuration }
    }
}

/// Request parameters for the [`get_futures_lead_trader_status`] operation.
///
/// This struct holds all of the inputs you can pass when calling
/// [`get_futures_lead_trader_status`](#method.get_futures_lead_trader_status).
#[derive(Clone, Debug, Builder, Default)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct GetFuturesLeadTraderStatusParams {
    ///
    /// The `recv_window` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub recv_window: Option<i64>,
}

impl GetFuturesLeadTraderStatusParams {
    /// Create a builder for [`get_futures_lead_trader_status`].
    ///
    #[must_use]
    pub fn builder() -> GetFuturesLeadTraderStatusParamsBuilder {
        GetFuturesLeadTraderStatusParamsBuilder::default()
    }
}
/// Request parameters for the [`get_futures_lead_trading_symbol_whitelist`] operation.
///
/// This struct holds all of the inputs you can pass when calling
/// [`get_futures_lead_trading_symbol_whitelist`](#method.get_futures_lead_trading_symbol_whitelist).
#[derive(Clone, Debug, Builder, Default)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct GetFuturesLeadTradingSymbolWhitelistParams {
    ///
    /// The `recv_window` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub recv_window: Option<i64>,
}

impl GetFuturesLeadTradingSymbolWhitelistParams {
    /// Create a builder for [`get_futures_lead_trading_symbol_whitelist`].
    ///
    #[must_use]
    pub fn builder() -> GetFuturesLeadTradingSymbolWhitelistParamsBuilder {
        GetFuturesLeadTradingSymbolWhitelistParamsBuilder::default()
    }
}

#[async_trait]
impl FutureCopyTradingApi for FutureCopyTradingApiClient {
    async fn get_futures_lead_trader_status(
        &self,
        params: GetFuturesLeadTraderStatusParams,
    ) -> anyhow::Result<RestApiResponse<models::GetFuturesLeadTraderStatusResponse>> {
        let GetFuturesLeadTraderStatusParams { recv_window } = params;

        let mut query_params = BTreeMap::new();
        let body_params = BTreeMap::new();

        if let Some(rw) = recv_window {
            query_params.insert("recvWindow".to_string(), json!(rw));
        }

        send_request::<models::GetFuturesLeadTraderStatusResponse>(
            &self.configuration,
            "/sapi/v1/copyTrading/futures/userStatus",
            reqwest::Method::GET,
            query_params,
            body_params,
            if HAS_TIME_UNIT {
                self.configuration.time_unit
            } else {
                None
            },
            true,
        )
        .await
    }

    async fn get_futures_lead_trading_symbol_whitelist(
        &self,
        params: GetFuturesLeadTradingSymbolWhitelistParams,
    ) -> anyhow::Result<RestApiResponse<models::GetFuturesLeadTradingSymbolWhitelistResponse>> {
        let GetFuturesLeadTradingSymbolWhitelistParams { recv_window } = params;

        let mut query_params = BTreeMap::new();
        let body_params = BTreeMap::new();

        if let Some(rw) = recv_window {
            query_params.insert("recvWindow".to_string(), json!(rw));
        }

        send_request::<models::GetFuturesLeadTradingSymbolWhitelistResponse>(
            &self.configuration,
            "/sapi/v1/copyTrading/futures/leadSymbol",
            reqwest::Method::GET,
            query_params,
            body_params,
            if HAS_TIME_UNIT {
                self.configuration.time_unit
            } else {
                None
            },
            true,
        )
        .await
    }
}

#[cfg(all(test, feature = "copy_trading"))]
mod tests {
    use super::*;
    use crate::TOKIO_SHARED_RT;
    use crate::{errors::ConnectorError, models::DataFuture, models::RestApiRateLimit};
    use async_trait::async_trait;
    use std::collections::HashMap;

    struct DummyRestApiResponse<T> {
        inner: Box<dyn FnOnce() -> DataFuture<Result<T, ConnectorError>> + Send + Sync>,
        status: u16,
        headers: HashMap<String, String>,
        rate_limits: Option<Vec<RestApiRateLimit>>,
    }

    impl<T> From<DummyRestApiResponse<T>> for RestApiResponse<T> {
        fn from(dummy: DummyRestApiResponse<T>) -> Self {
            Self {
                data_fn: dummy.inner,
                status: dummy.status,
                headers: dummy.headers,
                rate_limits: dummy.rate_limits,
            }
        }
    }

    struct MockFutureCopyTradingApiClient {
        force_error: bool,
    }

    #[async_trait]
    impl FutureCopyTradingApi for MockFutureCopyTradingApiClient {
        async fn get_futures_lead_trader_status(
            &self,
            _params: GetFuturesLeadTraderStatusParams,
        ) -> anyhow::Result<RestApiResponse<models::GetFuturesLeadTraderStatusResponse>> {
            if self.force_error {
                return Err(
                    ConnectorError::ConnectorClientError("ResponseError".to_string()).into(),
                );
            }

            let resp_json: Value = serde_json::from_str(r#"{"code":"000000","message":"success","data":{"isLeadTrader":true,"time":1717382310843},"success":true}"#).unwrap();
            let dummy_response: models::GetFuturesLeadTraderStatusResponse =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into models::GetFuturesLeadTraderStatusResponse");

            let dummy = DummyRestApiResponse {
                inner: Box::new(move || Box::pin(async move { Ok(dummy_response) })),
                status: 200,
                headers: HashMap::new(),
                rate_limits: None,
            };

            Ok(dummy.into())
        }

        async fn get_futures_lead_trading_symbol_whitelist(
            &self,
            _params: GetFuturesLeadTradingSymbolWhitelistParams,
        ) -> anyhow::Result<RestApiResponse<models::GetFuturesLeadTradingSymbolWhitelistResponse>>
        {
            if self.force_error {
                return Err(
                    ConnectorError::ConnectorClientError("ResponseError".to_string()).into(),
                );
            }

            let resp_json: Value = serde_json::from_str(r#"{"code":"000000","message":"success","data":[{"symbol":"BTCUSDT","baseAsset":"BTC","quoteAsset":"USDT"},{"symbol":"ETHUSDT","baseAsset":"ETH","quoteAsset":"USDT"}]}"#).unwrap();
            let dummy_response: models::GetFuturesLeadTradingSymbolWhitelistResponse =
                serde_json::from_value(resp_json.clone()).expect(
                    "should parse into models::GetFuturesLeadTradingSymbolWhitelistResponse",
                );

            let dummy = DummyRestApiResponse {
                inner: Box::new(move || Box::pin(async move { Ok(dummy_response) })),
                status: 200,
                headers: HashMap::new(),
                rate_limits: None,
            };

            Ok(dummy.into())
        }
    }

    #[test]
    fn get_futures_lead_trader_status_required_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockFutureCopyTradingApiClient { force_error: false };

            let params = GetFuturesLeadTraderStatusParams::builder().build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"code":"000000","message":"success","data":{"isLeadTrader":true,"time":1717382310843},"success":true}"#).unwrap();
            let expected_response : models::GetFuturesLeadTraderStatusResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetFuturesLeadTraderStatusResponse");

            let resp = client.get_futures_lead_trader_status(params).await.expect("Expected a response");
            let data_future = resp.data();
            let actual_response = data_future.await.unwrap();
            assert_eq!(actual_response, expected_response);
        });
    }

    #[test]
    fn get_futures_lead_trader_status_optional_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockFutureCopyTradingApiClient { force_error: false };

            let params = GetFuturesLeadTraderStatusParams::builder().recv_window(5000).build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"code":"000000","message":"success","data":{"isLeadTrader":true,"time":1717382310843},"success":true}"#).unwrap();
            let expected_response : models::GetFuturesLeadTraderStatusResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetFuturesLeadTraderStatusResponse");

            let resp = client.get_futures_lead_trader_status(params).await.expect("Expected a response");
            let data_future = resp.data();
            let actual_response = data_future.await.unwrap();
            assert_eq!(actual_response, expected_response);
        });
    }

    #[test]
    fn get_futures_lead_trader_status_response_error() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockFutureCopyTradingApiClient { force_error: true };

            let params = GetFuturesLeadTraderStatusParams::builder().build().unwrap();

            match client.get_futures_lead_trader_status(params).await {
                Ok(_) => panic!("Expected an error"),
                Err(err) => {
                    assert_eq!(err.to_string(), "Connector client error: ResponseError");
                }
            }
        });
    }

    #[test]
    fn get_futures_lead_trading_symbol_whitelist_required_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockFutureCopyTradingApiClient { force_error: false };

            let params = GetFuturesLeadTradingSymbolWhitelistParams::builder().build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"code":"000000","message":"success","data":[{"symbol":"BTCUSDT","baseAsset":"BTC","quoteAsset":"USDT"},{"symbol":"ETHUSDT","baseAsset":"ETH","quoteAsset":"USDT"}]}"#).unwrap();
            let expected_response : models::GetFuturesLeadTradingSymbolWhitelistResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetFuturesLeadTradingSymbolWhitelistResponse");

            let resp = client.get_futures_lead_trading_symbol_whitelist(params).await.expect("Expected a response");
            let data_future = resp.data();
            let actual_response = data_future.await.unwrap();
            assert_eq!(actual_response, expected_response);
        });
    }

    #[test]
    fn get_futures_lead_trading_symbol_whitelist_optional_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockFutureCopyTradingApiClient { force_error: false };

            let params = GetFuturesLeadTradingSymbolWhitelistParams::builder().recv_window(5000).build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"code":"000000","message":"success","data":[{"symbol":"BTCUSDT","baseAsset":"BTC","quoteAsset":"USDT"},{"symbol":"ETHUSDT","baseAsset":"ETH","quoteAsset":"USDT"}]}"#).unwrap();
            let expected_response : models::GetFuturesLeadTradingSymbolWhitelistResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetFuturesLeadTradingSymbolWhitelistResponse");

            let resp = client.get_futures_lead_trading_symbol_whitelist(params).await.expect("Expected a response");
            let data_future = resp.data();
            let actual_response = data_future.await.unwrap();
            assert_eq!(actual_response, expected_response);
        });
    }

    #[test]
    fn get_futures_lead_trading_symbol_whitelist_response_error() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockFutureCopyTradingApiClient { force_error: true };

            let params = GetFuturesLeadTradingSymbolWhitelistParams::builder()
                .build()
                .unwrap();

            match client
                .get_futures_lead_trading_symbol_whitelist(params)
                .await
            {
                Ok(_) => panic!("Expected an error"),
                Err(err) => {
                    assert_eq!(err.to_string(), "Connector client error: ResponseError");
                }
            }
        });
    }
}
