use lettre::AsyncTransport;

use crate::domain::api::ports::UserNotifier;

use crate::domain::api::models::user::*;
use crate::BASE_URL;

use super::DangerousLettre;

impl UserNotifier for DangerousLettre {
    async fn user_created(&self, user: &User, token: &ActivationToken) {
        let mut context = tera::Context::new();

        let url = format!("{}/auth/activate/{}", BASE_URL, token); // Move base url to env

        context.insert("activate_url", &url);

        let to = format!("{}", user.email()).parse().unwrap();

        let message = self
            .template("user/created", "Welcome to AVAM!", &context, to)
            .unwrap();

        if let Err(e) = self.mailer.send(message).await {
            eprintln!("{:#?}", e);
        };
    }

    async fn forgot_password(&self, user: &User, token: &PasswordResetToken) {
        let mut context = tera::Context::new();

        let url = format!("{}/auth/reset/{}", BASE_URL, token); // Move base url to env

        context.insert("reset_url", &url);

        let to = format!("{}", user.email()).parse().unwrap();

        let message = self
            .template(
                "user/password_reset",
                "Password reset request",
                &context,
                to,
            )
            .unwrap();

        if let Err(e) = self.mailer.send(message).await {
            eprintln!("{:#?}", e);
        };
    }
}
