use async_graphql::{InputObject, SimpleObject};

use crate::services::email_templates::EmailTemplate;

/// Email template type
#[derive(SimpleObject)]
pub struct EmailTemplateType {
    pub id: String,
    pub name: String,
    pub description: String,
    pub subject_template: String,
    pub body_template: String,
    /// Variable names used in this template
    pub variables: Vec<String>,
    /// Content type (text/html or text/plain)
    pub content_type: String,
}

impl From<&EmailTemplate> for EmailTemplateType {
    fn from(t: &EmailTemplate) -> Self {
        Self {
            id: t.id.clone(),
            name: t.name.clone(),
            description: t.description.clone(),
            subject_template: t.subject_template.clone(),
            body_template: t.body_template.clone(),
            variables: t.variable_names(),
            content_type: t.content_type.clone(),
        }
    }
}

/// Input for sending an email
#[derive(InputObject)]
pub struct SendEmailInput {
    /// Recipient email addresses
    pub to: Vec<String>,
    /// CC recipients
    pub cc: Option<Vec<String>>,
    /// BCC recipients
    pub bcc: Option<Vec<String>>,
    /// Email subject
    pub subject: String,
    /// Email body
    pub body: String,
    /// Content type (defaults to "text/html")
    pub content_type: Option<String>,
    /// Display name for the sender
    pub from_name: Option<String>,
}

/// Template variable input
#[derive(InputObject)]
pub struct TemplateVariableInput {
    pub name: String,
    pub value: String,
}

/// Input for sending a template email
#[derive(InputObject)]
pub struct SendTemplateEmailInput {
    /// Recipient email addresses
    pub to: Vec<String>,
    /// CC recipients
    pub cc: Option<Vec<String>>,
    /// BCC recipients
    pub bcc: Option<Vec<String>>,
    /// Template ID to use
    pub template_id: String,
    /// Template variables
    pub variables: Vec<TemplateVariableInput>,
    /// Display name for the sender
    pub from_name: Option<String>,
}

/// Result of sending an email
#[derive(SimpleObject)]
pub struct SendEmailResult {
    /// Whether the email was sent successfully
    pub success: bool,
    /// Gmail message ID (if successful)
    pub message_id: Option<String>,
    /// Gmail thread ID (if successful)
    pub thread_id: Option<String>,
    /// Error message (if failed)
    pub error: Option<String>,
}
