use crate::KondutoClient;
use crate::days_to_expire::DaysToExpire;
use crate::errors::error::KondutoResult;
use crate::sdk::decision_list::http::requests::DecisionListEntryRequest;
use crate::sdk::decision_list::http::responses::update::UpdateDecisionListEntryResponse;

impl KondutoClient {
    pub async fn update_days_to_expire(
        &self,
        request: DecisionListEntryRequest,
        days_to_expire: DaysToExpire,
    ) -> KondutoResult<UpdateDecisionListEntryResponse> {
        let endpoint = request.update_endpoint().to_string();
        let payload = request.update_expire_days_payload(days_to_expire);
        self.put(&endpoint, &payload).await
    }
}

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

    #[tokio::test]
    pub async fn test_update_item_in_list() -> Result<(), Box<dyn std::error::Error>> {
        let ctx = TestContext::new().await;
        let email = Email::try_from("test@mail.com")?;
        let request = DecisionListEntryRequest::email(DecisionListKind::Blocklist, email, None);
        
        let response = assert_api_success(
            ctx.client.update_days_to_expire(request, DaysToExpire::new(4)?).await
        );
        assert_eq!(response.status, StatusResponse::Ok);
        Ok(())
    }
}
