<h1>Entitlements — <code class="mono"><%= planTier %></code></h1>
<p style="color:var(--muted);font-size:12px;margin-top:-8px">
  Feature-access matrix for firms on this plan tier. Saving here also pushes the effective matrix to the
  tenant backend, so it takes effect immediately for every firm on this tier — no redeploy needed.
</p>

<% if (forbidden) { %>
  <div class="alert alert-danger">Your role does not have permission to edit plan entitlements.</div>
<% } else { %>

<% if (error) { %><div class="alert alert-danger"><%= error %></div><% } %>
<% if (saved) { %><div class="alert alert-success">Entitlements saved and synced to the tenant backend.</div><% } %>

<div class="card" style="max-width:560px">
  <form method="post" action="/billing/plans/<%= encodeURIComponent(planTier) %>/entitlements">
    <table>
      <thead><tr><th>Feature</th><th>Access</th></tr></thead>
      <tbody>
        <% const NUMERIC_KEYS = new Set(['storageLimitGb', 'workflow_templates.limit']); %>
        <% for (const key of featureKeys) { %>
          <input type="hidden" name="featureKeys" value="<%= key %>">
          <tr>
            <td><code class="mono"><%= key %></code></td>
            <td>
              <% if (NUMERIC_KEYS.has(key)) { %>
                <input type="number" min="0" name="limit__<%= key %>"
                       value="<%= matrix[key] === null || matrix[key] === undefined ? '' : matrix[key] %>"
                       placeholder="blank = unlimited" style="width:140px">
              <% } else { %>
                <label style="display:flex;align-items:center;gap:6px;margin:0">
                  <input type="checkbox" name="enabled__<%= key %>" value="true" <%= matrix[key] ? 'checked' : '' %>>
                  Enabled
                </label>
              <% } %>
            </td>
          </tr>
        <% } %>
        <% if (!featureKeys.length) { %><tr><td colspan="2" style="color:var(--muted)">No known feature keys.</td></tr><% } %>
      </tbody>
    </table>
    <div style="display:flex;gap:12px;margin-top:16px">
      <button class="btn" type="submit" style="flex:1">Save & Sync to Tenant Backend</button>
      <a href="/billing/plans" class="btn" style="flex:1;text-align:center;background:#2a2e38">Cancel</a>
    </div>
  </form>
</div>

<% } %>
