use loco_rs::prelude::*;
use serde::{Deserialize, Serialize};

use crate::services::subscriptions;

pub struct NotificationDeliveryWorker {
    pub ctx: AppContext,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct NotificationDeliveryWorkerArgs {
    pub delivery_id: i32,
}

#[async_trait]
impl BackgroundWorker<NotificationDeliveryWorkerArgs> for NotificationDeliveryWorker {
    fn build(ctx: &AppContext) -> Self {
        Self { ctx: ctx.clone() }
    }

    fn tags() -> Vec<String> {
        vec!["notifications".to_string()]
    }

    async fn perform(&self, args: NotificationDeliveryWorkerArgs) -> Result<()> {
        subscriptions::process_delivery(&self.ctx, args.delivery_id).await
    }
}
