/**
 * Source unique de verite des tarifs vitrine (gel statique, decision user
 * 2026-06-16). Les montants reffletent la table `pricing_tiers` de missioflow-app
 * (database/schema.sql) : facturation DEGRESSIVE MARGINALE (`tiers_mode=graduated`).
 *
 * « Graduated » = chaque tranche est facturee a SON taux, comme les tranches
 * d'impot — PAS un taux unique applique a tout l'effectif. L'algo ci-dessous
 * est le miroir exact de PlanService::computeGraduatedCents (cote app).
 *
 * A resynchroniser si les montants changent cote app (pricing_tiers).
 */

export interface PricingTier {
  /** Borne basse inclusive de la tranche (1, 11, 26). */
  readonly from: number;
  /** Borne haute inclusive, ou null pour l'infini (derniere tranche). */
  readonly to: number | null;
  /** Prix HT par utilisateur actif et par mois, en euros. */
  readonly unitPrice: number;
}

export const PRICING_TIERS: readonly PricingTier[] = [
  { from: 1, to: 10, unitPrice: 42 },
  { from: 11, to: 25, unitPrice: 36 },
  { from: 26, to: null, unitPrice: 30 },
];

/**
 * Total mensuel HT (en euros) pour `users` utilisateurs actifs, en tarif
 * gradue/marginal. Miroir de PlanService::computeGraduatedCents.
 * Plancher a 1 utilisateur (un tenant a toujours au moins son admin).
 */
export function computeMonthlyTotal(
  users: number,
  tiers: readonly PricingTier[] = PRICING_TIERS,
): number {
  const n = Math.max(1, Math.floor(users));
  let total = 0;
  for (const tier of tiers) {
    if (n < tier.from) continue;
    const hi = tier.to ?? n;
    const countInTier = Math.min(n, hi) - tier.from + 1;
    if (countInTier > 0) total += countInTier * tier.unitPrice;
  }
  return total;
}
