import Stripe from "stripe";
import { getSiteSetting } from "@/lib/site-settings";

/** Single Stripe client construction point — mirrors lib/ai/factory.ts's
 * provider-factory pattern (one place resolves the secret from
 * site-settings, DB taking precedence over env, everything else imports
 * the resolved client from here). Returns null if unconfigured so callers
 * can fail with a clear "billing not configured" message rather than a
 * cryptic Stripe SDK error. */
export async function getStripeClient(): Promise<Stripe | null> {
  const secretKey = await getSiteSetting("STRIPE_SECRET_KEY");
  if (!secretKey) return null;
  return new Stripe(secretKey);
}

export function isStripeTestMode(secretKey: string): boolean {
  return secretKey.startsWith("sk_test_");
}
