use crate::client::EuroMail;
use crate::errors::EuroMailError;
use crate::types::{
    Email, EmailDetail, ListParams, PaginatedResponse, SendBatchParams, SendBatchResponse,
    SendEmailParams, SendEmailResponse,
};

impl EuroMail {
    pub async fn send_email(
        &self,
        params: &SendEmailParams,
    ) -> Result<SendEmailResponse, EuroMailError> {
        self.post("/v1/emails", params).await
    }

    /// Batch send does NOT wrap in a data envelope - the response itself is `{ data: [...], errors: [...] }`.
    pub async fn send_batch(
        &self,
        params: &SendBatchParams,
    ) -> Result<SendBatchResponse, EuroMailError> {
        self.post_direct("/v1/emails/batch", params).await
    }

    pub async fn get_email(&self, email_id: &str) -> Result<EmailDetail, EuroMailError> {
        self.get(&format!("/v1/emails/{email_id}")).await
    }

    pub async fn list_emails(
        &self,
        params: Option<&ListParams>,
        status: Option<&str>,
    ) -> Result<PaginatedResponse<Email>, EuroMailError> {
        let mut query: Vec<(&str, String)> = Vec::new();
        if let Some(p) = params {
            if let Some(page) = p.page {
                query.push(("page", page.to_string()));
            }
            if let Some(per_page) = p.per_page {
                query.push(("per_page", per_page.to_string()));
            }
        }
        if let Some(s) = status {
            query.push(("status", s.to_string()));
        }
        self.get_with_query("/v1/emails", &query).await
    }
}
