<% content_for :body_class, "admin-body" %>

<section class="kpi-grid">
  <div class="kpi-card kpi-card--accent">
    <div class="kpi-label">Sessioni attive</div>
    <div class="kpi-value kpi-value--live" id="kpi-active-sessions"><%= @stats[:active_sessions] %></div>
    <div class="kpi-sub">in diretta ora</div>
  </div>
  <div class="kpi-card">
    <div class="kpi-label">Trasmesse oggi</div>
    <div class="kpi-value" id="kpi-sessions-today"><%= @stats[:sessions_today] %></div>
    <div class="kpi-sub"><%= format_duration_minutes(@stats[:stream_minutes_today]) %> di streaming</div>
  </div>
  <div class="kpi-card">
    <div class="kpi-label">Trasmesse questo mese</div>
    <div class="kpi-value" id="kpi-sessions-month"><%= @stats[:sessions_month] %></div>
    <div class="kpi-sub"><%= format_duration_minutes(@stats[:stream_minutes_month]) %> totali</div>
  </div>
  <div class="kpi-card">
    <div class="kpi-label">Squadre</div>
    <div class="kpi-value"><%= @stats[:teams_count] %></div>
    <div class="kpi-sub"><%= @stats[:users_count] %> utenti</div>
  </div>
  <div class="kpi-card">
    <div class="kpi-label">CPU</div>
    <div class="kpi-value" id="kpi-cpu"><%= @host[:cpu] %>%</div>
    <div class="kpi-sub">load <%= @host[:load_avg]["1m"] %> / <%= @host[:load_avg]["5m"] %> / <%= @host[:load_avg]["15m"] %></div>
  </div>
  <div class="kpi-card">
    <div class="kpi-label">RAM</div>
    <div class="kpi-value" id="kpi-mem"><%= @host[:memory][:used_percent] %>%</div>
    <div class="kpi-sub"><%= format_bytes(@host[:memory][:used_bytes]) %> / <%= format_bytes(@host[:memory][:total_bytes]) %></div>
  </div>
  <div class="kpi-card">
    <div class="kpi-label">Banda (totale)</div>
    <div class="kpi-value" style="font-size:1.2rem" id="kpi-network"><%= format_bytes(@host[:network][:total_bytes]) %></div>
    <div class="kpi-sub">↑ <%= format_bytes(@host[:network][:tx_bytes_total]) %> · ↓ <%= format_bytes(@host[:network][:rx_bytes_total]) %></div>
  </div>
  <div class="kpi-card">
    <div class="kpi-label">Replay in archivio</div>
    <div class="kpi-value"><%= @stats[:recordings_ready] %></div>
    <div class="kpi-sub"><%= @stats[:recordings_count] %> registrazioni</div>
  </div>
</section>

<section class="charts-grid">
  <div class="chart-card">
    <h3>CPU (%)</h3>
    <div class="chart-wrap"><canvas id="chart-cpu"></canvas></div>
  </div>
  <div class="chart-card">
    <h3>RAM (%)</h3>
    <div class="chart-wrap"><canvas id="chart-mem"></canvas></div>
  </div>
</section>

<section class="charts-grid" style="grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));">
  <div class="chart-card">
    <h3>Disco sistema</h3>
    <% root = @host[:disk][:root] %>
    <% if root[:total_bytes] %>
      <div class="disk-row">
        <header><span><%= root[:path] %></span><span><%= format_bytes(root[:used_bytes]) %> / <%= format_bytes(root[:total_bytes]) %></span></header>
        <div class="progress-bar <%= 'progress-bar--ok' if root[:used_percent].to_f < 80 %>"><span style="width:<%= root[:used_percent] %>%"></span></div>
        <p class="kpi-sub" style="margin-top:0.35rem">Libero: <%= format_bytes(root[:free_bytes]) %> (<%= root[:used_percent] %>% usato)</p>
      </div>
    <% else %>
      <p class="empty">Metriche disco non disponibili</p>
    <% end %>
  </div>
  <div class="chart-card">
    <h3>Archivio registrazioni</h3>
    <% rec = @host[:disk][:recordings] %>
    <% if rec[:total_bytes] %>
      <div class="disk-row">
        <header><span><%= rec[:path] %></span><span><%= format_bytes(rec[:used_bytes]) %> / <%= format_bytes(rec[:total_bytes]) %></span></header>
        <div class="progress-bar <%= 'progress-bar--ok' if rec[:used_percent].to_f < 80 %>"><span style="width:<%= rec[:used_percent] %>%"></span></div>
        <% if rec[:dir_size_bytes] %>
          <p class="kpi-sub" style="margin-top:0.35rem">Contenuto registrazioni: <%= format_bytes(rec[:dir_size_bytes]) %></p>
        <% end %>
      </div>
    <% else %>
      <p class="empty">Percorso <%= Admin::HostMetrics::RECORDINGS_PATH %> non montato</p>
    <% end %>
  </div>
</section>

<section class="admin-panels">
  <div class="panel">
    <h2>Sessioni attive</h2>
    <% if @active_sessions.any? %>
      <table class="admin-table">
        <thead>
          <tr><th>Partita</th><th>Stato</th><th>Inizio</th><th></th></tr>
        </thead>
        <tbody id="active-sessions-body">
          <% @active_sessions.each do |s| %>
            <tr>
              <td><%= s.match.team.name %> vs <%= s.match.opponent_name %></td>
              <td><span class="badge badge--<%= s.status == 'live' ? 'live' : (s.status == 'paused' ? 'paused' : 'connecting') %>"><%= s.status %></span></td>
              <td class="muted"><%= s.started_at&.strftime("%d/%m %H:%M") || "—" %></td>
              <td class="admin-actions">
                <%= link_to "Dettaglio", admin_session_path(s) %>
                <%= button_to "Termina", stop_admin_session_path(s), method: :post, class: "admin-btn admin-btn--danger admin-btn--sm", form: { data: { turbo_confirm: "Terminare questa sessione?" } } %>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>
    <% else %>
      <p class="empty">Nessuna sessione attiva in questo momento.</p>
    <% end %>
  </div>

  <div class="panel">
    <h2>Squadre</h2>
    <ul class="team-list">
      <% @teams.each do |t| %>
        <li>
          <%= link_to t.name, admin_team_path(t) %>
          <span class="muted"><%= t.matches.count %> partite</span>
        </li>
      <% end %>
    </ul>
    <% if @stats[:teams_count] > @teams.size %>
      <p class="kpi-sub" style="margin-top:0.75rem"><%= link_to "Vedi tutte (#{@stats[:teams_count]})", admin_teams_path %></p>
    <% end %>
  </div>
</section>

<script>
  window.adminMetricsUrl = "<%= admin_metrics_path %>";
  window.adminInitialHistory = <%= raw @host[:history].to_json %>;
</script>
