//! Email service module for handling email sending functionality
//!
//! This module provides a comprehensive email system that supports:
//! - Multiple email providers (SMTP, SendGrid, etc.)
//! - Template-based emails with Tera
//! - Form submissions and contact forms
//! - Email notifications
//! - Secure configuration management

#[cfg(feature = "leptos-hydration")]
pub mod contact_adapter;
pub mod providers;
pub mod service;
pub mod templates;
pub mod types;

#[cfg(feature = "leptos-hydration")]
pub use contact_adapter::ContactFormAdapter;
pub use providers::{ConsoleProvider, SendGridProvider, SmtpProvider};
pub use service::{EmailService, EmailServiceBuilder};
pub use templates::EmailTemplateEngine;
use thiserror::Error;
pub use types::{
    EmailConfig, EmailMessage, EmailProvider, FormSubmission, SendGridConfig, SmtpConfig,
};

#[derive(Error, Debug)]
pub enum EmailError {
    #[error("Email configuration error: {0}")]
    Config(String),

    #[error("Template error: {0}")]
    Template(String),

    #[error("SMTP error: {0}")]
    Smtp(String),

    #[error("SendGrid error: {0}")]
    SendGrid(String),

    #[error("Validation error: {0}")]
    Validation(String),

    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Serialization error: {0}")]
    Serialization(#[from] serde_json::Error),

    #[error("HTTP error: {0}")]
    Http(#[from] reqwest::Error),

    #[error("Generic error: {0}")]
    Generic(String),
}

impl EmailError {
    pub fn config(msg: &str) -> Self {
        EmailError::Config(msg.to_string())
    }

    pub fn template(msg: &str) -> Self {
        EmailError::Template(msg.to_string())
    }

    pub fn smtp(msg: &str) -> Self {
        EmailError::Smtp(msg.to_string())
    }

    pub fn sendgrid(msg: &str) -> Self {
        EmailError::SendGrid(msg.to_string())
    }

    pub fn validation(msg: &str) -> Self {
        EmailError::Validation(msg.to_string())
    }

    pub fn generic(msg: &str) -> Self {
        EmailError::Generic(msg.to_string())
    }
}

pub type EmailResult<T> = Result<T, EmailError>;
