import { describe, it, expect } from "vitest";
import { computeMonthlyTotal, PRICING_TIERS } from "./pricing.js";

describe("computeMonthlyTotal() — tarif gradue marginal (42/36/30)", () => {
  it("facture le plancher d'1 utilisateur", () => {
    expect(computeMonthlyTotal(1)).toBe(42);
    expect(computeMonthlyTotal(0)).toBe(42); // plancher a 1
    expect(computeMonthlyTotal(-5)).toBe(42);
  });

  it("reste dans la 1re tranche (1-10) a 42 € chacun", () => {
    expect(computeMonthlyTotal(5)).toBe(210);
    expect(computeMonthlyTotal(10)).toBe(420);
  });

  it("applique le taux marginal a la 2e tranche (11-25)", () => {
    expect(computeMonthlyTotal(11)).toBe(420 + 36); // 456
    expect(computeMonthlyTotal(15)).toBe(420 + 5 * 36); // 600
    expect(computeMonthlyTotal(25)).toBe(420 + 15 * 36); // 960
  });

  it("applique le taux marginal a la 3e tranche (26+)", () => {
    expect(computeMonthlyTotal(26)).toBe(960 + 30); // 990
    expect(computeMonthlyTotal(30)).toBe(420 + 15 * 36 + 5 * 30); // 1110
  });

  it("ne facture jamais au taux global (pas de tarif volume)", () => {
    // 15 utilisateurs : graduated = 600, pas 15 * 36 = 540
    expect(computeMonthlyTotal(15)).not.toBe(15 * 36);
  });

  it("a une grille coherente (tranches contigues, prix decroissants)", () => {
    expect(PRICING_TIERS).toHaveLength(3);
    expect(PRICING_TIERS[0].from).toBe(1);
    expect(PRICING_TIERS.at(-1)?.to).toBeNull();
    for (let i = 1; i < PRICING_TIERS.length; i++) {
      expect(PRICING_TIERS[i].from).toBe((PRICING_TIERS[i - 1].to ?? 0) + 1);
      expect(PRICING_TIERS[i].unitPrice).toBeLessThan(PRICING_TIERS[i - 1].unitPrice);
    }
  });
});
