// edition.jsx — frontend mirror of the Edition Entitlement Engine (PART 4).
//
// Reads the active edition from the bootstrap (window.DATA.edition, populated by the server's
// editionInfo()) and answers Edition.has(featureKey) so the UI gates coherently — showing
// "Upgrade Required" instead of a broken screen or a raw 403. The SERVER (route guard +
// automation guards) is the real enforcement; this is UX coherence only.
//
// master (default) => isMaster true => Edition.has() always true => ZERO UI change.
(function () {
  const info = () => (window.DATA && window.DATA.edition) || { edition: "master", label: "Maxab Master", isMaster: true, features: [], allFeatures: [] };
  const has = (key) => { if (!key) return true; const e = info(); return !!e.isMaster || (e.features || []).indexOf(key) >= 0; };
  // route -> required feature (UI-level mirror of the server routeFeatureMap). Only the clearly-gated nav.
  const NAV_FEATURE = { campaigns: "campaigns", learning: "learning_hub", head: "directors" };
  const navFeature = (route /*, tab, director */) => NAV_FEATURE[route] || null;
  // lowest edition that unlocks a feature, for the upgrade label (mirrors server upgradeOrder)
  const REQ = {
    campaigns: "Entry", follow_up: "Entry", quotation: "Entry",
    customer_journey: "Growth", sales_funnel: "Growth", discounts: "Growth", long_term_memory: "Growth", learning_hub: "Growth",
    payments: "Professional", booking: "Professional", directors: "Professional", trusted_advisor: "Professional", business_assets: "Professional", customer_success: "Professional",
    company_os: "Enterprise", operations_os: "Enterprise", revenue_operations: "Enterprise", business_intelligence: "Enterprise", autonomous_execution: "Enterprise", multi_provider_routing: "Enterprise",
    reseller_rights: "Reseller",
  };
  const upgrade = (key) => "Available in the " + (REQ[key] || "a higher") + " edition — Upgrade Required.";
  window.Edition = { info, has, navFeature, upgrade, isMaster: () => info().isMaster, label: () => info().label, edition: () => info().edition };
})();

// <FeatureGate feature="payments">…</FeatureGate> — renders children only if the active edition has the
// feature; otherwise an inline upgrade note (or nothing, with note={false}). Use to gate buttons/sections.
function FeatureGate({ feature, children, note }) {
  if (window.Edition.has(feature)) return children;
  if (note === false) return null;
  return (
    <div className="subtle" style={{ fontSize: 12, padding: 10, border: "1px dashed var(--border-1)", borderRadius: 8, textAlign: "center" }}>
      {window.Edition.upgrade(feature)}
    </div>
  );
}
window.FeatureGate = FeatureGate;
