use loco_rs::prelude::*;

use crate::services::subscriptions;

pub struct RetryDeliveries;

#[async_trait]
impl Task for RetryDeliveries {
    fn task(&self) -> TaskInfo {
        TaskInfo {
            name: "retry_deliveries".to_string(),
            detail: "enqueue due notification deliveries for retry".to_string(),
        }
    }

    async fn run(&self, app_context: &AppContext, vars: &task::Vars) -> Result<()> {
        let limit = vars
            .cli
            .get("limit")
            .and_then(|value| value.parse::<u64>().ok())
            .unwrap_or(200);
        let queued = subscriptions::retry_due_deliveries(app_context, limit).await?;
        tracing::info!("retry_deliveries queued {queued} jobs");
        Ok(())
    }
}
