/**
 * Single source of truth for "does this user currently have Pro access".
 *
 * Deliberately checks `expiresAt` in addition to `plan` so that access cuts
 * off exactly at the end of a subscription's current billing period (the
 * grace period after a cancel-at-period-end request) even if the Stripe
 * webhook that would flip `plan` back to "free" is delayed or lost.
 */

/** Minimal structural view of a user row used by the Pro-access check. */
export interface PlanExpiryLike {
  plan: string;
  expiresAt: Date | null;
}

/**
 * True when `user` currently has Pro access. `expiresAt: null` means no
 * expiry (lifetime plan, or a plan that has never been time-boxed) — never
 * "already expired".
 */
export function isProActive(user: PlanExpiryLike | null | undefined): boolean {
  if (!user || user.plan === "free") return false;
  if (!user.expiresAt) return true;
  return user.expiresAt.getTime() > Date.now();
}
