<% content_for :title, "#{@team.name} — Dashboard" %>

<div class="wrap" style="padding-top:20px">
  <h1><%= @team.name %></h1>
  <p>
    Piano: <strong><%= @entitlements.plan.name %></strong>
    · Staff trasmissione: <strong><%= @entitlements.staff_count_for("transmission") %> / <%= @entitlements.max_staff_transmission || "∞" %></strong>
    · Staff regia: <strong><%= @entitlements.staff_count_for("regia") %> / <%= @entitlements.max_staff_regia || "∞" %></strong>
    · Partite attive: <strong><%= @entitlements.concurrent_streams_used %><% if @entitlements.concurrent_streams_limit %> / <%= @entitlements.concurrent_streams_limit %><% else %> (illimitate)<% end %></strong>
  </p>

  <div style="display:flex;gap:12px;flex-wrap:wrap;margin:20px 0">
    <%= link_to "Abbonamento", public_team_billing_path(@team), class: "btn btn-primary" %>
    <%= link_to "Aggiungi staff", public_team_invite_path(@team), class: "btn btn-secondary" %>
    <%= link_to "Dirette live", public_live_index_path, class: "btn btn-secondary" %>
  </div>

  <h2>Staff attivo</h2>
  <div class="card">
    <% if @members.any? %>
      <table class="data">
        <thead><tr><th>Nome</th><th>Email</th><th>Ruolo</th><th></th></tr></thead>
        <tbody>
          <% @members.each do |ut| %>
            <tr>
              <td><%= ut.user.name %></td>
              <td><%= ut.user.email %></td>
              <td><%= ut.staff_kind == "regia" ? "Regia" : "Trasmissione" %></td>
              <td>
                <%= button_to "Revoca", public_team_remove_member_path(@team, ut.user), method: :delete, class: "btn btn-secondary", style: "padding:4px 10px;font-size:0.8rem", form: { data: { turbo_confirm: "Revocare l'accesso a #{ut.user.name}?" } } %>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>
    <% else %>
      <p style="color:#888">Nessun membro staff. <%= link_to "Aggiungi qualcuno", public_team_invite_path(@team) %>.</p>
    <% end %>
  </div>

  <h2>Staff in attesa</h2>
  <div class="card">
    <% if @pending_invitations.any? %>
      <table class="data">
        <thead><tr><th>Email</th><th>Ruolo</th><th>Scade</th><th></th></tr></thead>
        <tbody>
          <% @pending_invitations.each do |inv| %>
            <tr>
              <td><%= inv.email %></td>
              <td><%= inv.staff_kind == "regia" ? "Regia" : "Trasmissione" %></td>
              <td><%= l inv.expires_at, format: :short %></td>
              <td>
                <%= button_to "Annulla", public_team_destroy_invitation_path(@team, inv), method: :delete, class: "btn btn-secondary", style: "padding:4px 10px;font-size:0.8rem" %>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>
    <% else %>
      <p style="color:#888">Nessuna richiesta in sospeso.</p>
    <% end %>
  </div>

  <% if @entitlements.can_access_recordings? && @recordings.any? %>
    <h2>Archivio gare</h2>
    <div class="card">
      <table class="data">
        <thead><tr><th>Partita</th><th>Replay</th></tr></thead>
        <tbody>
          <% @recordings.each do |rec| %>
            <tr>
              <td><%= rec.stream_session.match.team.name %> vs <%= rec.stream_session.match.opponent_name %></td>
              <td><%= link_to "Guarda", rec.replay_url %></td>
            </tr>
          <% end %>
        </tbody>
      </table>
    </div>
  <% end %>

  <p style="color:#888;margin-top:24px">Usa l'app mobile con le stesse credenziali per avviare partite e dirette.</p>
</div>
