/**
 * Server-side price catalog — the single source of truth for what a plan costs.
 *
 * The client only ever sends a plan identifier ("weekly" | "annual" | "lifetime").
 * Amounts, trial lengths, billing intervals and access windows are decided HERE,
 * never in the frontend. Consumers:
 *
 *   - /api/checkout            → builds the Stripe Checkout session from the
 *                                catalog (env Price ID, or price_data with the
 *                                catalog amount as fallback).
 *   - /api/webhooks/stripe     → cross-checks the amount actually paid against
 *                                the catalog before granting a license.
 *
 * This module is shared infrastructure: do NOT add feature code here, and do
 * NOT change amounts without also updating the Stripe dashboard prices.
 */

export type PlanId = "weekly" | "annual" | "lifetime";

export const PLAN_IDS: readonly PlanId[] = ["weekly", "annual", "lifetime"];

export interface PlanConfig {
  id: PlanId;
  /** Env var holding the Stripe Price ID, e.g. STRIPE_WEEKLY_PRICE_ID */
  priceIdEnv: string;
  /** Expected amount in minor units (cents) — never trust the client */
  unitAmountMinor: number;
  /** Stripe Checkout mode */
  mode: "subscription" | "payment";
  /** Trial days for subscription plans (weekly pass) */
  trialPeriodDays?: number;
  /** Billing interval for subscription plans */
  interval?: "week" | "year";
  /** Days of access granted from the payment; null = lifetime (never expires) */
  expiryDays: number | null;
  /** Human label used in sale alerts */
  label: string;
}

export const PLAN_CONFIGS: Record<PlanId, PlanConfig> = {
  weekly: {
    id: "weekly",
    priceIdEnv: "STRIPE_WEEKLY_PRICE_ID",
    unitAmountMinor: 499, // 4.99 €
    mode: "subscription",
    trialPeriodDays: 3,
    interval: "week",
    // 3 days trial + 7 days standard billing interval = 10 days initial window
    expiryDays: 10,
    label: "Wochen-Pass (4,99 €/Woche, 3 Tage Trial)",
  },
  annual: {
    id: "annual",
    priceIdEnv: "STRIPE_ANNUAL_PRICE_ID",
    unitAmountMinor: 3999, // 39.99 €
    mode: "subscription",
    interval: "year",
    expiryDays: 365,
    label: "Jahres-Pro (39,99 €/Jahr)",
  },
  lifetime: {
    id: "lifetime",
    priceIdEnv: "STRIPE_LIFETIME_PRICE_ID",
    unitAmountMinor: 5999, // 59.99 €
    mode: "payment",
    expiryDays: null, // never expires
    label: "Lifetime Lizenz (59,99 €)",
  },
};

/**
 * Resolves a client-supplied plan string to a known plan id. Unknown or absent
 * values fall back to the annual default instead of inventing a plan — a
 * bogus plan must never silently change what the customer is charged.
 */
export function resolvePlan(plan: string | null | undefined): PlanId {
  return plan === "weekly" || plan === "annual" || plan === "lifetime"
    ? plan
    : "annual";
}

export function getPlanConfig(plan: string | null | undefined): PlanConfig {
  return PLAN_CONFIGS[resolvePlan(plan)];
}

/** True when `plan` is one of the known ids. */
export function isPlanId(plan: string | null | undefined): plan is PlanId {
  return plan === "weekly" || plan === "annual" || plan === "lifetime";
}
