use serde::Serialize;

use crate::client::EuroMail;
use crate::errors::EuroMailError;
use crate::types::{Domain, DomainVerification, ListParams, PaginatedResponse};

#[derive(Serialize)]
struct AddDomainBody {
    domain: String,
}

impl EuroMail {
    pub async fn add_domain(&self, domain: &str) -> Result<Domain, EuroMailError> {
        let body = AddDomainBody {
            domain: domain.to_string(),
        };
        self.post("/v1/domains", &body).await
    }

    pub async fn get_domain(&self, domain_id: &str) -> Result<Domain, EuroMailError> {
        self.get(&format!("/v1/domains/{domain_id}")).await
    }

    pub async fn verify_domain(
        &self,
        domain_id: &str,
    ) -> Result<DomainVerification, EuroMailError> {
        self.post_direct(&format!("/v1/domains/{domain_id}/verify"), &serde_json::json!({}))
            .await
    }

    pub async fn delete_domain(&self, domain_id: &str) -> Result<(), EuroMailError> {
        self.delete(&format!("/v1/domains/{domain_id}")).await
    }

    pub async fn list_domains(
        &self,
        params: Option<&ListParams>,
    ) -> Result<PaginatedResponse<Domain>, 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()));
            }
        }
        self.get_with_query("/v1/domains", &query).await
    }
}
