<% layout('layouts/main') %>
<% const companyStats = metrics.companies || {}; %>
<% const userStats = metrics.users || {}; %>
<% const subscriptionStats = metrics.subscriptions || {}; %>
<% const planLabel = { trial: 'Essai', starter: 'Starter', business: 'Business', premium: 'Premium' }; %>

<section class="saas-admin-page">
  <div class="saas-admin-layout">
    <aside class="saas-admin-rail">
      <div class="rail-brand"><span>SA</span><strong>Admin SaaS</strong></div>
      <nav>
        <a class="active" href="/saas-admin">Clients SaaS</a>
        <a href="/tarifs">Tarifs publics</a>
        <a href="/auth/entreprise">Créer une entreprise</a>
      </nav>
      <% if (!saasOwnerEmailsConfigured) { %>
        <p class="rail-warning">Accès propriétaire réservé aux administrateurs de la plateforme.</p>
      <% } %>
    </aside>

    <main class="saas-admin-main">
      <div class="saas-admin-head">
        <div>
          <p class="eyebrow">Plateforme</p>
          <h1>Gestion des clients SaaS</h1>
          <p>Vue propriétaire pour piloter les entreprises clientes, abonnements, domaines, support et activité globale.</p>
        </div>
        <a class="button" href="/auth/entreprise">Créer une entreprise</a>
      </div>

      <section class="saas-admin-stats">
        <article><span>Entreprises</span><strong><%= companyStats.total || 0 %></strong><small><%= companyStats.active || 0 %> actives</small></article>
        <article><span>MRR</span><strong><%= ui.money(subscriptionStats.mrr || 0) %></strong><small><%= subscriptionStats.active || 0 %> abonnements actifs</small></article>
        <article><span>Utilisateurs</span><strong><%= userStats.total || 0 %></strong><small><%= userStats.clients || 0 %> clients finaux</small></article>
        <article><span>Domaines DNS</span><strong><%= metrics.pendingDomains.total || 0 %></strong><small>En attente</small></article>
        <article><span>Support</span><strong><%= metrics.support.support_high || 0 %></strong><small>Priorité haute</small></article>
      </section>

      <form method="get" class="panel saas-admin-filters">
        <label>Recherche<input name="q" value="<%= filters.q %>" placeholder="Entreprise, slug, email"></label>
        <label>Statut<select name="status">
          <option value="">Tous statuts</option>
          <% ['active','suspended','archived'].forEach((status) => { %><option value="<%= status %>" <%= filters.status === status ? 'selected' : '' %>><%= ui.label(status) %></option><% }) %>
        </select></label>
        <label>Plan<select name="plan">
          <option value="">Tous plans</option>
          <% ['trial','starter','business','premium'].forEach((plan) => { %><option value="<%= plan %>" <%= filters.plan === plan ? 'selected' : '' %>><%= planLabel[plan] %></option><% }) %>
        </select></label>
        <button>Filtrer</button>
      </form>

      <section class="saas-admin-table panel">
        <div class="table-title">
          <div><h2>Entreprises clientes</h2><p>Gestion plateforme: abonnement, support, statut, métriques d’usage et accès public.</p></div>
        </div>
        <div class="table-scroll">
          <table>
            <thead>
              <tr>
                <th>Entreprise</th>
                <th>Propriétaire</th>
                <th>Plan</th>
                <th>Usage</th>
                <th>CA clients</th>
                <th>Support</th>
                <th></th>
              </tr>
            </thead>
            <tbody>
              <% companies.forEach((company) => { %>
                <tr>
                  <td>
                    <strong><%= company.name %></strong>
                    <span><%= company.slug %> · <%= ui.label(company.status) %></span>
                  </td>
                  <td>
                    <strong><%= company.owner_first_name || '' %> <%= company.owner_last_name || '' %></strong>
                    <span><%= company.owner_email || company.email || 'Non renseigné' %></span>
                  </td>
                  <td>
                    <strong><%= planLabel[company.plan_code] || 'Essai' %></strong>
                    <span><%= ui.label(company.subscription_status || 'trialing') %> · <%= ui.money(company.monthly_price || 0) %></span>
                  </td>
                  <td>
                    <strong><%= company.clients_count || 0 %> clients</strong>
                    <span><%= company.employees_count || 0 %> employés · <%= company.requests_count || 0 %> demandes</span>
                  </td>
                  <td><strong><%= ui.money(company.invoice_total || 0) %></strong><span>Facturé par l’entreprise</span></td>
                  <td><span class="badge"><%= company.support_priority || 'normale' %></span></td>
                  <td><a class="button secondary small" href="/saas-admin/entreprises/<%= company.id %>">Gérer</a></td>
                </tr>
              <% }) %>
              <% if (!companies.length) { %>
                <tr><td colspan="7">Aucune entreprise trouvée.</td></tr>
              <% } %>
            </tbody>
          </table>
        </div>
      </section>
    </main>
  </div>
</section>
