<% team = local_assigns[:team] %>
<% can_manage = local_assigns[:can_manage] %>
<% owner_membership = local_assigns[:owner_membership] %>
<% staff_memberships = local_assigns[:staff_memberships] %>
<% pending_invitations = local_assigns[:pending_invitations] %>
<% recordings = local_assigns[:recordings] %>
<% entitlements = local_assigns[:entitlements] %>

<section class="team-streaming-staff">
  <% if can_manage %>
    <% if owner_membership&.staff_kind.blank? %>
      <div class="card team-streaming-staff__self">
        <h2>Il tuo account (<%= current_user.email %>)</h2>
        <p class="muted">
          Puoi gestire la trasmissione dall’app. Per il punteggio da un altro telefono, condividi il
          <strong>link regia</strong> durante la diretta (non serve un secondo account).
        </p>
        <div class="team-details-actions">
          <%= button_to "Sono io — responsabile trasmissione", public_team_assign_self_staff_path(team), method: :post, params: { staff_kind: "transmission" }, class: "btn btn-primary" %>
        </div>
      </div>
    <% end %>
  <% end %>

  <h2>Responsabili trasmissione (app)</h2>
  <div class="card">
    <% if staff_memberships.any? %>
      <table class="data">
        <thead><tr><th>Nome</th><th>Email</th><th>Ruolo</th><% if can_manage %><th></th><% end %></tr></thead>
        <tbody>
          <% staff_memberships.each do |ut| %>
            <tr>
              <td>
                <%= ut.user.name %>
                <% if ut.user_id == current_user.id %><span class="muted"> (tu)</span><% end %>
              </td>
              <td><%= ut.user.email %></td>
              <td>Trasmissione</td>
              <% if can_manage %>
                <td>
                  <% if ut.user_id == current_user.id && team.club.owned_by?(current_user) %>
                    <%= button_to "Rimuovi ruolo staff", public_team_clear_self_staff_path(team), method: :delete, class: "btn btn-secondary", style: "padding:4px 10px;font-size:0.8rem", form: { data: { turbo_confirm: "Rimuovere il ruolo staff dal tuo account?" } } %>
                  <% elsif ut.role == "member" %>
                    <%= 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}?" } } %>
                  <% end %>
                </td>
              <% end %>
            </tr>
          <% end %>
        </tbody>
      </table>
    <% else %>
      <p class="muted">
        Nessun responsabile trasmissione.
        <% if can_manage %><%= link_to "Invita qualcuno", public_team_invite_path(team) %>.<% end %>
      </p>
    <% end %>
  </div>

  <% if can_manage %>
    <h2>Inviti in attesa</h2>
    <div class="card">
      <% if pending_invitations.any? %>
        <table class="data">
          <thead><tr><th>Email</th><th>Scade</th><th></th></tr></thead>
          <tbody>
            <% pending_invitations.each do |inv| %>
              <tr>
                <td><%= inv.email %></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 class="muted">Nessuna richiesta in sospeso.</p>
      <% end %>
    </div>
  <% end %>

  <% 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 %>
</section>
