// Phase 2 limit-coverage track — feature-gate helper.
//
// Single source of truth for the 5-mode UX matrix that governs all
// limit-aware features. Backend mirrors it: AbstractCountLimitGuard +
// AbstractTwoLevelQuotaGuard enforce the same rules server-side; this
// helper just shapes the UI affordance.
//
// 5-mode rules (see docs/limit-coverage-implementation-plan.md §1.2):
//   A) limit=0, count=0           → A-mode (default — feature off)
//   B) limit=0, count>0           → B-mode (admin tightened, rows linger)
//   C) limit=N, count<N (or -1)   → C-mode (normal)
//   D) limit=N, count===N         → D-mode (cap reached)
//   E) limit=N, count>N           → E-mode (over-cap; admin shrunk plan)
//
// Categories tune A-mode behaviour:
//   - 'advanced-opt-in'    → A-mode HIDES the feature (catchall, wblist, filters)
//   - 'flow-crud'          → A-mode keeps tab, disables Create (mailbox, FTP, ...)
//   - 'always-on-override' → A-mode keeps tab, override-CTA disabled (spam check)
//
// Returns a single render-shape:
//   { mode, visible, rowsMuted, createBtn, editBtn, deleteBtn, tooltip }
//
// createBtn / editBtn / deleteBtn: 'enabled' | 'disabled' | 'hidden'.
// Edit + Delete are ALWAYS enabled (the contract: only Create can grow
// the count). The fields are kept on the return shape so call sites can
// destructure uniformly without checks.

function featureGate({ limit, count, category = 'flow-crud' }) {
  const limitNum = (limit === undefined || limit === null) ? -1 : Number(limit);
  const countNum = Number.isFinite(count) ? Number(count) : 0;

  let mode;
  if (limitNum < 0)                  mode = 'C'; // unlimited == C semantics
  else if (limitNum === 0)           mode = countNum === 0 ? 'A' : 'B';
  else if (countNum < limitNum)      mode = 'C';
  else if (countNum === limitNum)    mode = 'D';
  else                               mode = 'E';

  const isAdvancedOptIn = category === 'advanced-opt-in';

  // visibility: only advanced-opt-in features hide in A-mode (clean
  // default for clients who don't have the feature in their plan at all).
  let visible = true;
  if (mode === 'A' && isAdvancedOptIn) visible = false;

  // rows muted: B-mode leaves rows in-place but signals they're frozen
  // (no new ones can be added). Useful for advanced features the admin
  // disabled while data still exists.
  const rowsMuted = mode === 'B';

  // createBtn: disabled in A (when shown), B, D, E. C is the only enabled.
  let createBtn = 'enabled';
  if (mode === 'A' && !isAdvancedOptIn) createBtn = 'disabled';
  if (mode === 'B' || mode === 'D' || mode === 'E') createBtn = 'disabled';

  // edit + delete: ALWAYS enabled (lets clients shrink toward the limit
  // or refine existing rows — see §1.3 backend contract).
  const editBtn = 'enabled';
  const deleteBtn = 'enabled';

  // Tooltips (Hungarian — matches LIMIT_REACHED_TOOLTIP pattern).
  let tooltip = null;
  if (createBtn === 'disabled') {
    if (mode === 'A') {
      tooltip = 'A csomagod nem tartalmazza ezt a lehetőséget.';
    } else if (mode === 'B') {
      tooltip = 'A csomagod jelenleg nem engedi újabb létrehozását. Töröld a meglévőket vagy fordulj az adminisztrátorhoz.';
    } else if (mode === 'D') {
      tooltip = `Elérted a csomag ${limitNum}-es limitjét.`;
    } else if (mode === 'E') {
      tooltip = `${countNum} létezik, de a csomag csak ${limitNum}-t enged. Törölj ${countNum - limitNum} darabot.`;
    }
  }

  return { mode, visible, rowsMuted, createBtn, editBtn, deleteBtn, tooltip };
}

window.featureGate = featureGate;
