<%
  const hasOwn = (name) => Object.prototype.hasOwnProperty.call(policy || {}, name);
  const policyMode = (name) => !hasOwn(name) ? (allowInherit ? "inherit" : "off") : policy[name] === false ? "off" : "custom";
  const windowValue = policy?.window && policy.window !== false ? policy.window : {};
  const streamValue = policy?.perStream && policy.perStream !== false ? policy.perStream : {};
  const requirementValue = policy?.requirement && policy.requirement !== false ? policy.requirement : {};
  const costValue = policy?.cost && policy.cost !== false ? policy.cost : {};
  const fieldPrefix = typeof namePrefix === "string" ? namePrefix : "";
  const fieldName = (name) => `${fieldPrefix}${name}`;
  const effectiveValues = typeof effective === "object" && effective ? effective : {};
  const inheritOptionLabel = typeof inheritLabel === "string" && inheritLabel ? inheritLabel : "Use group/default";
  const inheritedText = (name, fallback) => effectiveValues[name] || fallback;
%>
<div class="command-policy-grid">
  <fieldset class="subsection command-policy-section" data-policy-category data-policy-name="window">
    <legend>Rate limit</legend>
    <label>Setting
      <select name="<%= fieldName('window_mode') %>" data-policy-mode>
        <% if (allowInherit) { %><option value="inherit" <%= policyMode("window") === "inherit" ? "selected" : "" %>><%= inheritOptionLabel %></option><% } %>
        <option value="off" <%= policyMode("window") === "off" ? "selected" : "" %>>No time-window limit</option>
        <option value="custom" <%= policyMode("window") === "custom" ? "selected" : "" %>>Set a limit</option>
      </select>
    </label>
    <% if (allowInherit) { %><div class="command-policy-inherited" data-policy-inherited="window"><span>Currently applies</span><strong><%= inheritedText("window", "This category keeps each command's built-in value.") %></strong></div><% } %>
    <div class="command-policy-values" data-policy-values>
      <label>Uses<input name="<%= fieldName('window_uses') %>" type="number" min="1" step="1" value="<%= windowValue.uses || 1 %>" /></label>
      <label>Within seconds<input name="<%= fieldName('window_seconds') %>" type="number" min="1" step="1" value="<%= windowValue.seconds || 30 %>" /></label>
      <label>Count<select name="<%= fieldName('window_scope') %>"><option value="user" <%= windowValue.scope !== "global" ? "selected" : "" %>>Per user</option><option value="global" <%= windowValue.scope === "global" ? "selected" : "" %>>Everyone together</option></select></label>
    </div>
  </fieldset>
  <fieldset class="subsection command-policy-section" data-policy-category data-policy-name="perStream">
    <legend>Per stream</legend>
    <label>Setting
      <select name="<%= fieldName('perStream_mode') %>" data-policy-mode>
        <% if (allowInherit) { %><option value="inherit" <%= policyMode("perStream") === "inherit" ? "selected" : "" %>><%= inheritOptionLabel %></option><% } %>
        <option value="off" <%= policyMode("perStream") === "off" ? "selected" : "" %>>No stream limit</option>
        <option value="custom" <%= policyMode("perStream") === "custom" ? "selected" : "" %>>Set a limit</option>
      </select>
    </label>
    <% if (allowInherit) { %><div class="command-policy-inherited" data-policy-inherited="perStream"><span>Currently applies</span><strong><%= inheritedText("perStream", "This category keeps each command's built-in value.") %></strong></div><% } %>
    <div class="command-policy-values" data-policy-values>
      <label>Uses per stream<input name="<%= fieldName('perStream_uses') %>" type="number" min="1" step="1" value="<%= streamValue.uses || 1 %>" /></label>
      <label>Count<select name="<%= fieldName('perStream_scope') %>"><option value="user" <%= streamValue.scope !== "global" ? "selected" : "" %>>Per user</option><option value="global" <%= streamValue.scope === "global" ? "selected" : "" %>>Everyone together</option></select></label>
    </div>
    <p class="hint">A restarted stream stays in the same session for two hours, so a crash does not reset this limit.</p>
  </fieldset>
  <fieldset class="subsection command-policy-section" data-policy-category data-policy-name="requirement">
    <legend>Who may use it</legend>
    <label>Setting
      <select name="<%= fieldName('requirement_mode') %>" data-policy-mode>
        <% if (allowInherit) { %><option value="inherit" <%= policyMode("requirement") === "inherit" ? "selected" : "" %>><%= inheritOptionLabel %></option><% } %>
        <option value="off" <%= policyMode("requirement") === "off" ? "selected" : "" %>>Everyone</option>
        <option value="custom" <%= policyMode("requirement") === "custom" ? "selected" : "" %>>Require a role</option>
      </select>
    </label>
    <% if (allowInherit) { %><div class="command-policy-inherited" data-policy-inherited="requirement"><span>Currently applies</span><strong><%= inheritedText("requirement", "This category keeps each command's built-in value.") %></strong></div><% } %>
    <div class="command-policy-values" data-policy-values>
      <label>Minimum access<select name="<%= fieldName('requirement_role') %>" data-requirement-role>
        <% [["follower","Follower"],["subscriber","Subscriber / member"],["vip","VIP"],["mod","Moderator"],["editor","Editor"],["streamer","Streamer only"]].forEach(([value,label]) => { %>
        <option value="<%= value %>" <%= requirementValue.role === value ? "selected" : "" %>><%= label %></option>
        <% }) %>
      </select></label>
      <label data-subscriber-tier>Subscriber tier<select name="<%= fieldName('requirement_subscriber_tier') %>"><option value="1" <%= Number(requirementValue.subscriberTier || 1) === 1 ? "selected" : "" %>>Any tier</option><option value="2" <%= Number(requirementValue.subscriberTier) === 2 ? "selected" : "" %>>Tier 2+</option><option value="3" <%= Number(requirementValue.subscriberTier) === 3 ? "selected" : "" %>>Tier 3</option></select></label>
    </div>
  </fieldset>
  <fieldset class="subsection command-policy-section" data-policy-category data-policy-name="cost">
    <legend>Currency cost</legend>
    <label>Setting
      <select name="<%= fieldName('cost_mode') %>" data-policy-mode>
        <% if (allowInherit) { %><option value="inherit" <%= policyMode("cost") === "inherit" ? "selected" : "" %>><%= inheritOptionLabel %></option><% } %>
        <option value="off" <%= policyMode("cost") === "off" ? "selected" : "" %>>Free</option>
        <option value="custom" <%= policyMode("cost") === "custom" ? "selected" : "" %>>Charge currency</option>
      </select>
    </label>
    <% if (allowInherit) { %><div class="command-policy-inherited" data-policy-inherited="cost"><span>Currently applies</span><strong><%= inheritedText("cost", "This category keeps each command's built-in value.") %></strong></div><% } %>
    <div class="command-policy-values" data-policy-values>
      <label>Cost<input name="<%= fieldName('cost_amount') %>" type="number" min="1" step="1" value="<%= costValue.amount || 1 %>" /></label>
    </div>
    <p class="hint">The Economy Framework deducts and logs this like any other banking transaction.</p>
  </fieldset>
</div>
