import type { BillingPeriod } from "@/components/sections/pricing-data";

export type PlanId = "free" | "pro" | "business";

export type PaidPlanId = Extract<PlanId, "pro" | "business">;

export const PAID_PLAN_IDS: PaidPlanId[] = ["pro", "business"];

export function isPaidPlanId(value: string): value is PaidPlanId {
  return PAID_PLAN_IDS.includes(value as PaidPlanId);
}

export function getPlanLabel(plan: PlanId): string {
  switch (plan) {
    case "pro":
      return "Pro";
    case "business":
      return "Business";
    default:
      return "Free";
  }
}

export function getStripePriceId(
  plan: PaidPlanId,
  billing: BillingPeriod
): string {
  const priceIds: Record<PaidPlanId, Record<BillingPeriod, string | undefined>> =
    {
      pro: {
        monthly: process.env.STRIPE_PRICE_PRO_MONTHLY,
        annual: process.env.STRIPE_PRICE_PRO_ANNUAL,
      },
      business: {
        monthly: process.env.STRIPE_PRICE_BUSINESS_MONTHLY,
        annual: process.env.STRIPE_PRICE_BUSINESS_ANNUAL,
      },
    };

  const priceId = priceIds[plan][billing];

  if (!priceId) {
    throw new Error(
      `Missing Stripe price ID for plan "${plan}" (${billing} billing).`
    );
  }

  return priceId;
}
