// US-009: Trading settings endpoints

use crate::futures::models::account::{SetLeverageResponse, StatusMsgResponse};
use crate::rest::client::RestClient;
use crate::rest::error::AsterDexError;
use crate::rest::response::ApiResponse;

impl RestClient {
    /// Set leverage for a symbol.
    pub async fn set_leverage(
        &self,
        symbol: &str,
        leverage: u32,
    ) -> Result<ApiResponse<SetLeverageResponse>, AsterDexError> {
        let lev_str = leverage.to_string();
        self.signed_post("/fapi/v3/leverage", &[
            ("symbol", symbol),
            ("leverage", &lev_str),
        ]).await
    }

    /// Set margin type (ISOLATED or CROSSED) for a symbol.
    pub async fn set_margin_type(
        &self,
        symbol: &str,
        margin_type: &str,
    ) -> Result<ApiResponse<StatusMsgResponse>, AsterDexError> {
        self.signed_post("/fapi/v3/marginType", &[
            ("symbol", symbol),
            ("marginType", margin_type),
        ]).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::auth::Credentials;

    // US-009 test 3: set_leverage with mock returns leverage == 20
    #[tokio::test]
    async fn set_leverage_mock() {
        let mut server = mockito::Server::new_async().await;
        let _mock = server
            .mock("POST", "/fapi/v3/leverage")
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(r#"{"leverage":20,"maxNotionalValue":"1000000","symbol":"BTCUSDT"}"#)
            .create_async()
            .await;

        let creds = Credentials::new(
            "0x0000000000000000000000000000000000000001",
            "0x0000000000000000000000000000000000000002",
            "0000000000000000000000000000000000000000000000000000000000000001",
            714,
        )
        .unwrap();
        let client = RestClient::new(&server.url(), creds).unwrap();
        let resp = client.set_leverage("BTCUSDT", 20).await.unwrap();
        assert_eq!(resp.data.leverage, 20);
    }
}
