use crate::app::config::Configuration;
use crate::mail;
use rocket::Build;
use rocket::Rocket;

pub fn init(config: &Configuration) -> super::Result<mail::Service> {
    let Configuration {
        from,
        base_uri,
        email_template_dir: tmpl_dir,
        ..
    } = config;

    (if let Some(path) = config.filemail_into.as_deref() {
        mail::Service::filemail(from, base_uri, tmpl_dir, path)
    } else if let Some(true) = config.local_smtp {
        mail::Service::localsmtp(from, base_uri, tmpl_dir)
    } else {
        mail::Service::sendmail(from, base_uri, tmpl_dir)
    })
    .map(Some)
}

pub fn register(
    rocket: Rocket<Build>,
    _config: &Configuration,
    state: mail::Service,
) -> Rocket<Build> {
    rocket.manage(state)
}
