<% layout('layouts/main') %>
<% const clientBase = typeof clientBaseUrl !== 'undefined' ? clientBaseUrl : '/client'; %>
<div class="app-shell">
  <%- partial('partials/client-nav') %>
  <section class="content">
    <div class="client-home-hero">
      <div>
        <p class="eyebrow">Espace client</p>
        <h1>Bonjour <%= currentUser.first_name %></h1>
        <p>Retrouvez vos demandes, devis, factures, interventions et messages avec <%= currentCompany ? currentCompany.name : 'votre entreprise' %>.</p>
      </div>
      <div class="client-next-card">
        <span>Prochaine intervention</span>
        <strong><%= nextIntervention ? nextIntervention.service_type : 'Aucune date planifiée' %></strong>
        <small><%= nextIntervention ? ui.date(nextIntervention.starts_at, true) : 'Vous serez averti dès qu’une intervention est confirmée.' %></small>
      </div>
    </div>
    <div class="stat-grid">
      <div class="stat"><strong><%= stats.requests.total %></strong><span>Demandes</span></div>
      <div class="stat"><strong><%= stats.quotes.total %></strong><span>Devis en attente</span></div>
      <div class="stat"><strong><%= stats.invoices.total %></strong><span>Factures à payer</span></div>
      <div class="stat"><strong><%= stats.documents.total %></strong><span>Documents</span></div>
      <div class="stat"><strong><%= stats.reports.total %></strong><span>Rapports</span></div>
    </div>
    <section class="panel guided-dashboard client-guided-dashboard">
      <div class="compact-heading">
        <p class="eyebrow">Que souhaitez-vous faire ?</p>
        <h2>Vos actions principales</h2>
        <p>Tout ce qui concerne votre demande, vos documents et vos échanges reste ici.</p>
      </div>
      <div class="guided-action-grid">
        <a href="<%= clientBase %>/demande-devis"><strong>Faire une nouvelle demande</strong><span>Expliquez le besoin et ajoutez des informations utiles.</span></a>
        <a href="<%= clientBase %>/devis"><strong>Voir mes devis</strong><span>Consultez, acceptez ou refusez les propositions reçues.</span></a>
        <a href="<%= clientBase %>/factures"><strong>Suivre mes factures</strong><span>Retrouvez les montants, statuts et documents PDF.</span></a>
        <a href="<%= clientBase %>/interventions"><strong>Suivre mes interventions</strong><span>Consultez les dates, avis et rapports publiés.</span></a>
        <a href="<%= clientBase %>/documents"><strong>Ouvrir mes documents</strong><span>Devis, factures, rapports et pièces partagées.</span></a>
        <a href="<%= clientBase %>/messages"><strong>Écrire un message</strong><span>Posez une question sans chercher l’historique ailleurs.</span></a>
      </div>
    </section>
    <div class="client-quick-actions">
      <a class="button" href="<%= clientBase %>/demande-devis">Demander un devis</a>
      <a class="button secondary" href="<%= clientBase %>/messages">Envoyer un message</a>
      <a class="button secondary" href="<%= clientBase %>/documents">Mes documents</a>
    </div>
    <div class="panel client-dashboard-card">
      <div class="page-head inline-head">
        <div>
          <p class="eyebrow">Documents partagés</p>
          <h2>Derniers PDF et rapports</h2>
        </div>
        <a class="button secondary small" href="<%= clientBase %>/documents">Tout voir</a>
      </div>
      <div class="mobile-list">
        <% latestDocuments.forEach(doc=>{ %>
          <article class="list-card">
            <div class="list-card-head"><div><strong><%= doc.number %></strong><span><%= doc.typeLabel %> · <%= doc.service_type || 'Document' %></span></div><span class="badge"><%= ui.label(doc.status) %></span></div>
            <span class="muted"><%= ui.date(doc.created_at, true) %></span>
            <a class="button secondary small" href="<%= doc.download_href %>">Ouvrir</a>
          </article>
        <% }) %>
        <% if (!latestDocuments.length) { %><p class="empty-state"><strong>Aucun document partagé</strong><span>Les devis, factures, rapports et pièces visibles client apparaîtront ici.</span></p><% } %>
      </div>
    </div>
    <div class="panel client-dashboard-card">
      <div class="page-head inline-head">
        <div>
          <p class="eyebrow">Historique</p>
          <h2>Dernières demandes</h2>
        </div>
        <a class="button secondary small" href="<%= clientBase %>/demandes">Tout voir</a>
      </div>
      <div class="mobile-list">
        <% latestRequests.forEach(r=>{ %>
          <article class="list-card">
            <div class="list-card-head"><strong><%= r.service_type %></strong><span class="badge"><%= ui.label(r.status) %></span></div>
            <span class="muted"><%= ui.date(r.created_at) %></span>
          </article>
        <% }) %>
        <% if (!latestRequests.length) { %><p class="empty-state"><strong>Aucune demande</strong><span>Votre historique apparaîtra ici après une demande de devis.</span></p><% } %>
      </div>
    </div>
  </section>
</div>
