<h1>Usage Analytics</h1>

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

<div class="card">
  <p style="color:var(--muted);font-size:12px">Computed live from real login/record data — churn risk = 14+ days with no login at the firm AND an active, non-comped subscription.</p>
  <table>
    <thead><tr><th>Firm</th><th>Users</th><th>Active (30d)</th><th>Matters</th><th>Clients</th><th>Last Login</th><th>Subscription</th><th>Risk</th></tr></thead>
    <tbody>
      <% for (const f of firms) { %>
      <tr>
        <td><%= f.firmName %></td>
        <td><%= f.totalUsers %></td>
        <td><%= f.activeUsers30d %></td>
        <td><%= f.matterCount %></td>
        <td><%= f.clientCount %></td>
        <td><%= f.lastLoginAt ? new Date(f.lastLoginAt).toLocaleDateString() : 'Never' %> <% if (f.daysSinceLastLogin != null) { %>(<%= f.daysSinceLastLogin %>d ago)<% } %></td>
        <td><%= f.subscriptionStatus %></td>
        <td><% if (f.churnRisk) { %><span class="badge badge-danger">Churn Risk</span><% } else if (f.atRisk) { %><span class="badge badge-warn">Inactive</span><% } else { %><span class="badge badge-ok">OK</span><% } %></td>
      </tr>
      <% } %>
      <% if (!firms.length) { %><tr><td colspan="8" style="color:var(--muted)">No firm data available.</td></tr><% } %>
    </tbody>
  </table>
</div>

<% } %>
