use config::AppConfig;
use queue::NatsProducer;

use crate::EmailMessage;

#[derive(Clone)]
pub struct AppEmail {
    producer: NatsProducer,
    topic: String,
}

impl AppEmail {
    pub async fn init(config: &AppConfig) -> anyhow::Result<Self> {
        Ok(Self {
            producer: NatsProducer::new(config).await?,
            topic: config.email_topic(),
        })
    }

    pub fn with_topic(
        producer: NatsProducer,
        topic: impl Into<String>,
    ) -> Self {
        Self {
            producer,
            topic: topic.into(),
        }
    }

    pub async fn send(&self, message: EmailMessage) -> anyhow::Result<()> {
        self.producer
            .send(&self.topic, &message.to, &message, None)
            .await
    }

    pub fn topic(&self) -> &str {
        &self.topic
    }
}
