"use node";

import Stripe from "stripe";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);

export async function getOrCreateStripeCustomer(input: {
  stripeCustomerId: string | undefined;
  email: string;
  name: string;
  convexUserId: string;
}): Promise<string> {
  if (input.stripeCustomerId) {
    return input.stripeCustomerId;
  }

  const customer = await stripe.customers.create({
    email: input.email,
    name: input.name,
    metadata: { convexUserId: input.convexUserId },
  });

  return customer.id;
}
