use crate::KondutoClient;
use crate::sdk::decision_list::http::requests::DecisionListEntryRequest;
use crate::sdk::decision_list::http::responses::create::CreateDecisionListEntryResponse;

impl KondutoClient {
    /// Adiciona um item a uma lista
    ///
    /// # Argumentos
    ///
    /// * `request` - struct com os dados de tipo de lista, tipo de entrada e valor a ser consultado
    ///
    /// # Exemplo
    ///
    /// ```no_run
    /// use konduto::{KondutoClient, DecisionEntryType};
    /// use konduto::kind::DecisionListKind;
    /// use konduto::sdk::api_key::KondutoApiKey;
    /// use reqwest::Url;
    /// use konduto::sdk::decision_list::http::requests::DecisionListEntryRequest;
    /// use konduto::email::Email;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let api_key = KondutoApiKey::new("sua_api_key")?;
    ///     let url = Url::parse("https://api.konduto.com")?;
    ///     let client = KondutoClient::with_base_url(api_key.clone(), url);
    ///     let email = Email::try_from("fromsdk@mail.com")?;
    ///     let request = DecisionListEntryRequest::email(DecisionListKind::Blocklist, email, None);
    ///     client.add_to_list(request).await?;
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn add_to_list(
        &self,
        request: DecisionListEntryRequest,
    ) -> crate::KondutoResult<CreateDecisionListEntryResponse> {
        let endpoint = request.create_endpoint().to_string();
        let payload = request.create_payload();
        self.post(&endpoint, &payload).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::email::Email;
    use crate::kind::DecisionListKind;
    use crate::sdk::decision_list::http::responses::create::StatusResponse;
    use crate::supports::tests::{TestContext, assert_api_success};

    #[tokio::test]
    pub async fn test_add_to_list() {
        let ctx = TestContext::new().await;
        let email = Email::try_from("fromsdk2@mail.com").expect("Invalid email");
        let request = DecisionListEntryRequest::email(DecisionListKind::Blocklist, email, None);
        
        let response = assert_api_success(ctx.client.add_to_list(request).await);
        assert_eq!(response.status, StatusResponse::Ok);
    }
}
