export const premiumPlans = {
  monthly: {
    id: "monthly",
    name: "Toolaro Premium Monatlich",
    price: "4,99 €",
    interval: "Monat",
    stripePriceIdEnv: "STRIPE_PRICE_MONTHLY",
  },
  yearly: {
    id: "yearly",
    name: "Toolaro Premium Jährlich",
    price: "39,99 €",
    interval: "Jahr",
    stripePriceIdEnv: "STRIPE_PRICE_YEARLY",
  },
} as const;

export type PremiumPlanId = keyof typeof premiumPlans;

export function getPlan(id: string): (typeof premiumPlans)[PremiumPlanId] | null {
  return id === "monthly" || id === "yearly" ? premiumPlans[id] : null;
}

export function stripeConfigured() {
  return Boolean(process.env.STRIPE_SECRET_KEY && process.env.STRIPE_PRICE_MONTHLY && process.env.STRIPE_PRICE_YEARLY);
}

export async function stripeRequest(path: string, params: Record<string, string>) {
  if (!process.env.STRIPE_SECRET_KEY) throw new Error("Stripe ist nicht konfiguriert.");
  const body = new URLSearchParams(params);
  const res = await fetch(`https://api.stripe.com/v1/${path}`, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.STRIPE_SECRET_KEY}`,
      "Content-Type": "application/x-www-form-urlencoded",
    },
    body,
  });
  const json = await res.json();
  if (!res.ok) throw new Error(json.error?.message ?? "Stripe-Anfrage fehlgeschlagen.");
  return json as Record<string, unknown>;
}
