import Stripe from "stripe";
import { isStripeConfigured } from "@/config/plans";

const globalForStripe = globalThis as typeof globalThis & {
  stripe?: Stripe;
};

/** Lazy Stripe client — safe when STRIPE_SECRET_KEY is unset at build time. */
export function getStripe(): Stripe | null {
  if (!isStripeConfigured()) return null;

  if (!globalForStripe.stripe) {
    globalForStripe.stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
  }

  return globalForStripe.stripe;
}
