import Stripe from "stripe";

import { env } from "@/lib/env";

let cachedStripe: Stripe | null = null;

/* Returns a singleton Stripe client pinned to API 2026-04-22.dahlia.
   Lazy so callers that never need Stripe don't crash if the key is
   missing in dev (matches the Resend helper pattern from Phase 4). */
export function getStripe(): Stripe {
  if (cachedStripe) return cachedStripe;
  if (!env.STRIPE_SECRET_KEY) {
    throw new Error("STRIPE_SECRET_KEY is not configured");
  }
  cachedStripe = new Stripe(env.STRIPE_SECRET_KEY, {
    apiVersion: "2026-04-22.dahlia",
    appInfo: { name: "eleva-speed", version: "0.1.0" },
  });
  return cachedStripe;
}
