//! Email transport and default templates.
//!
//! ## Transport
//!
//! Configure exactly one delivery mechanism by registering an [`EmailProvider`] on
//! [`crate::config::AuthConfig`] / [`crate::plugin::AuthContext`]. Implementations may
//! wrap SMTP, HTTP APIs (Resend, SendGrid, SES), or logging for development
//! ([`ConsoleEmailProvider`]).
//!
//! ## Templates
//!
//! Built-in plugins use [`templates`] for default subject/body copy. Replace that copy
//! by supplying plugin-specific `send_*` callbacks, or by reusing and adjusting the
//! helpers in your own provider. You can also override built-ins per tenant by
//! inserting active rows into `email_templates` (`template_key` + `locale`).

mod audit;
#[cfg(feature = "smtp")]
mod smtp;
mod templates;

pub use audit::DatabaseLoggingEmailProvider;
#[cfg(feature = "smtp")]
pub use smtp::{SmtpConfig, SmtpEmailProvider};
pub use templates::{
    AuthEmailBodies, AuthEmailEventCatalogEntry, AuthEmailRenderContext, AuthEmailTemplateKey,
    account_deletion_confirmation_email, account_deletion_confirmation_email_for_context,
    auth_email_event_catalog, change_email_confirmation_email,
    change_email_confirmation_email_for_context, default_event_type_for_template_key,
    default_template_key_for_event_type, organization_invitation_email,
    organization_invitation_email_for_context, password_reset_email,
    password_reset_email_for_context, render_auth_email_template, two_factor_otp_email,
    two_factor_otp_email_for_context, verification_email, verification_email_for_context,
};

use async_trait::async_trait;

use crate::error::AuthResult;

/// Log / metrics tag when email is sent via [`EmailProvider`] (not a custom plugin callback).
pub const DEFAULT_EMAIL_TRANSPORT_ID: &str = "email_provider";

/// Trait for sending emails. Implement this to integrate with your
/// email service (SMTP, Zeptomail etc).
#[async_trait]
pub trait EmailProvider: Send + Sync {
    /// Send an email.
    ///
    /// - `to`: recipient email address
    /// - `subject`: email subject line
    /// - `html`: HTML body (may be empty)
    /// - `text`: plain-text body (may be empty)
    async fn send(&self, to: &str, subject: &str, html: &str, text: &str) -> AuthResult<()>;
}

/// Development email provider that logs emails to stderr.
///
/// Useful for local development and testing — no external dependencies.
pub struct ConsoleEmailProvider;

#[async_trait]
impl EmailProvider for ConsoleEmailProvider {
    async fn send(&self, to: &str, subject: &str, _html: &str, text: &str) -> AuthResult<()> {
        eprintln!("[EMAIL] To: {to} | Subject: {subject} | Body: {text}");
        Ok(())
    }
}

#[cfg(test)]
#[path = "../__tests__/email.rs"]
mod tests;
