use super::StripePaymentProcessor;
use crate::{payments::Error, pbbson::Model};
use stripe_core::Customer;
use stripe_core::customer::ListCustomer;

pub(super) async fn find_customer_for_account<B: Clone + Send + Sync + ToString>(
    this: &StripePaymentProcessor<B>,
    account: &Model,
) -> Result<Customer, Error> {
    let email = account
        .get_str("email")
        .map_err(|e| Error::internal(e.to_string()))?
        .to_string();
    let req = ListCustomer::new().email(email);
    let customers = req.send(&this.client).await?.data;
    match customers.first() {
        None => Err(Error::not_found("No Stripe customer found for account")),
        Some(customer) => Ok(customer.clone()),
    }
}
