import Stripe from "stripe";

let stripe: Stripe | null = null;

export function getStripe(): Stripe {
  const key = process.env.STRIPE_SECRET_KEY;
  if (!key) {
    throw new Error("STRIPE_SECRET_KEY is not configured");
  }
  if (!stripe) {
    stripe = new Stripe(key);
  }
  return stripe;
}

export function isStripeConfigured(): boolean {
  return Boolean(process.env.STRIPE_SECRET_KEY);
}

/**
 * Bypass Stripe Checkout when:
 * - BILLING_DEV_MOCK=true, or
 * - development and no STRIPE_SECRET_KEY (so UI still works without stripe listen)
 * Set BILLING_DEV_MOCK=false to force real Stripe even without keys (will 503).
 */
export function isBillingDevMock(): boolean {
  if (process.env.BILLING_DEV_MOCK === "true") return true;
  if (process.env.BILLING_DEV_MOCK === "false") return false;
  return process.env.NODE_ENV === "development" && !process.env.STRIPE_SECRET_KEY;
}
