<% content_for :title, "Dettagli — #{@team.name}" %>
<% content_for :meta_description, "Dettagli squadra #{@team.name} su Match Live TV." %>
<% content_for :robots, "noindex, nofollow" %>
<% total_people = @roster_by_category.values.sum(&:size) %>

<div class="wrap team-roster-page">
  <nav class="team-dashboard-nav" aria-label="Percorso squadra">
    <% if @can_manage %>
      <%= link_to "← #{@club.name}", public_club_path(@club), class: "team-dashboard-nav__club" %>
      <span class="team-dashboard-nav__sep">·</span>
      <%= link_to "Modifica squadra", public_edit_team_path(@team) %>
    <% else %>
      <span class="team-dashboard-nav__club"><%= @club.name %></span>
    <% end %>
  </nav>

  <header class="roster-hero card">
    <div class="roster-hero__intro">
      <p class="roster-hero__club"><%= @club.name %></p>
      <h1 class="roster-hero__title"><%= @team.name %></h1>
      <p class="team-details-meta">
        Piano: <strong><%= @entitlements.plan.name %></strong> (società)
        · Responsabili trasmissione: <strong><%= @entitlements.staff_count_for("transmission") %> / <%= @entitlements.max_staff_transmission || "∞" %></strong>
        · Partite attive: <strong><%= @entitlements.concurrent_streams_used %><% if @entitlements.concurrent_streams_limit %> / <%= @entitlements.concurrent_streams_limit %><% else %> (illimitate)<% end %></strong>
      </p>
      <% if @team.description.present? %>
        <div class="roster-hero__desc"><%= simple_format @team.description %></div>
      <% elsif @can_manage %>
        <p class="roster-hero__desc roster-hero__desc--muted">
          Nessuna descrizione.
          <%= link_to "Aggiungila dalla modifica squadra", public_edit_team_path(@team) %>.
        </p>
      <% end %>
      <div class="roster-stats">
        <span class="roster-stat"><strong><%= total_people %></strong> in organico</span>
        <% TeamRosterMember::DISPLAY_ORDER.each do |cat| %>
          <% count = @roster_by_category[cat].size %>
          <% next if count.zero? %>
          <span class="roster-stat"><%= count %> <%= TeamRosterMember::CATEGORY_LABELS[cat].downcase %></span>
        <% end %>
      </div>
    </div>
    <div class="roster-hero__media">
      <% if @team.team_photo_url.present? %>
        <%= image_tag @team.team_photo_url, alt: @team.name, class: "roster-hero__photo" %>
      <% elsif @can_manage %>
        <div class="roster-hero__photo roster-hero__photo--empty">
          <span>Foto squadra non ancora caricata</span>
          <%= link_to "Aggiungi dalla modifica squadra", public_edit_team_path(@team), class: "btn btn-secondary", style: "margin-top:8px;padding:8px 14px;font-size:0.85rem" %>
        </div>
      <% end %>
    </div>
  </header>

  <div class="team-details-actions">
    <% if @can_manage %>
      <%= link_to "← Società", public_club_path(@club), class: "btn btn-secondary" %>
      <%= link_to "Responsabili trasmissione", public_team_invite_path(@team), class: "btn btn-secondary" %>
    <% end %>
    <% if current_user.can_stream_for?(@team) %>
      <%= link_to "Programma partite", public_team_matches_path(@team), class: "btn btn-primary" %>
    <% end %>
    <%= link_to "Dirette live", public_live_index_path(club_id: @club.id), class: "btn btn-secondary" %>
  </div>

  <div class="roster-layout<%= " roster-layout--readonly" unless @can_manage %>">
    <section class="roster-main" aria-label="Organico">
      <% TeamRosterMember::DISPLAY_ORDER.each do |category| %>
        <% members = @roster_by_category[category] %>
        <section class="roster-section card">
          <header class="roster-section__head">
            <h2 class="roster-section__title"><%= TeamRosterMember::CATEGORY_LABELS[category] %></h2>
            <span class="roster-section__count"><%= members.size %></span>
          </header>
          <% if members.any? %>
            <div class="roster-list">
              <% members.each do |person| %>
                <%= render "shared/roster_person_card", person: person, team: @team, editable: @can_manage %>
              <% end %>
            </div>
          <% else %>
            <p class="roster-section__empty">Nessuna persona in questa categoria.</p>
          <% end %>
        </section>
      <% end %>
    </section>

    <% if @can_manage %>
      <aside class="roster-sidebar" aria-label="Aggiungi persona">
        <%= render "shared/roster_member_form", member: @roster_member, team: @team %>
      </aside>
    <% end %>
  </div>

  <%= render "public/teams/streaming_staff", team: @team, club: @club, can_manage: @can_manage,
        entitlements: @entitlements, owner_membership: @owner_membership,
        staff_memberships: @staff_memberships, pending_invitations: @pending_invitations,
        recordings: @recordings %>

  <p class="team-details-footer">Usa l'app mobile con le stesse credenziali per avviare partite e dirette.</p>
</div>
