/**
 * Which pricing model an organization is on, persisted on `billing.pricing_model`.
 *
 * `seats` (default) is the legacy seat-based model: the Stripe subscription is
 * anchored on a `builder_seat` line item and AI usage is gated per assigned seat.
 * `credits` is the PLG model where the org buys credit packs (surfaced as "GAUs")
 * and deployed apps, and AI usage is metered against the org-level pool with no seats.
 *
 * Values match the PostgreSQL `pricing_model_enum`. Existing orgs default to `seats`
 * so they are grandfathered onto seat-based pricing until explicitly migrated.
 */
export enum PricingModelEnum {
  CREDITS = 'credits',
  SEATS = 'seats'
}

export type PricingModel = `${PricingModelEnum}`;

/**
 * True only when the org is on the seatless credits (GAU) pricing model. Anything
 * absent or unrecognized is treated as seat-based — the grandfather-safe default.
 */
export function isCreditsPricing(pricingModel: PricingModel | undefined | null): boolean {
  return pricingModel === PricingModelEnum.CREDITS;
}

/**
 * True when the org is on seat-based pricing — the inverse of {@link isCreditsPricing}.
 * Anything absent or unrecognized reads as seat-based, so use this rather than a raw
 * `=== 'seats'` comparison, which would miss the grandfathered (unset) case.
 */
export function isSeatsPricing(pricingModel: PricingModel | undefined | null): boolean {
  return !isCreditsPricing(pricingModel);
}

const KNOWN_PRICING_MODELS: ReadonlySet<string> = new Set(Object.values(PricingModelEnum));

/**
 * Coerce the raw value of the `superblocks.billing.default-pricing-model` LaunchDarkly
 * flag into a valid {@link PricingModel} for stamping onto a new org at creation. (Conversion
 * does not re-resolve the flag; it carries the existing stamp forward — see
 * `createBillingHistoryRowForOrg`.)
 *
 * Fails **closed to `seats`** for anything unrecognized — an LD typo, a variation the
 * running code doesn't know yet, or a wrong-typed value — so a misconfigured flag can
 * never strand a new org on a half-built pricing model.
 */
export function resolvePricingModel(rawFlagValue: unknown): PricingModel {
  return typeof rawFlagValue === 'string' && KNOWN_PRICING_MODELS.has(rawFlagValue)
    ? (rawFlagValue as PricingModel)
    : PricingModelEnum.SEATS;
}
