<% content_for :title, "Replay — #{@club.name}" %>
<% content_for :robots, "noindex, nofollow" %>

<div class="wrap club-dashboard" style="padding-top:20px">
  <%= link_to "← #{@club.name}", public_club_path(@club), class: "back-link" %>

  <h1>Archivio Replay</h1>
  <p class="muted">Gestisci registrazioni, visibilità, download e pubblicazione YouTube.</p>

  <div class="kpi-grid" style="display:grid;grid-template-columns:repeat(auto-fit,minmax(140px,1fr));gap:12px;margin:20px 0">
    <div class="card" style="padding:16px">
      <div class="kpi-value"><%= @stats[:available_count] %></div>
      <div class="kpi-sub">Replay disponibili</div>
    </div>
    <div class="card" style="padding:16px">
      <div class="kpi-value"><%= @stats[:expiring_soon_count] %></div>
      <div class="kpi-sub">In scadenza (7 gg)</div>
    </div>
    <div class="card" style="padding:16px">
      <div class="kpi-value"><%= number_to_human_size(@stats[:total_bytes]) %></div>
      <div class="kpi-sub">Spazio archivio</div>
    </div>
    <div class="card" style="padding:16px">
      <div class="kpi-value"><%= @stats[:total_views] %></div>
      <div class="kpi-sub">Visualizzazioni totali</div>
    </div>
  </div>

  <% if @recordings.any? %>
    <div class="card">
      <table class="data">
        <thead>
          <tr>
            <th></th>
            <th>Titolo</th>
            <th>Data</th>
            <th>Durata</th>
            <th>Views</th>
            <th>Scadenza</th>
            <th>Stato</th>
            <th>Visibilità</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <% @recordings.each do |rec| %>
            <tr>
              <td style="width:72px">
                <% if rec.thumbnail_url %>
                  <img src="<%= rec.thumbnail_url %>" alt="" width="64" height="36" style="object-fit:cover;border-radius:4px">
                <% end %>
              </td>
              <td><%= rec.title_or_default %></td>
              <td><%= l_local(rec.recorded_at_or_fallback) %></td>
              <td><%= rec.duration_label %></td>
              <td><%= rec.view_count %></td>
              <td><%= rec.expires_at ? l_local(rec.expires_at) : "—" %></td>
              <td><%= rec.status_label %></td>
              <td>
                <%= form_with url: public_club_recording_path(@club, rec), method: :patch, local: true do %>
                  <%= select_tag "recording[privacy_status]",
                      options_for_select([["Pubblico", "public"], ["Privato", "unlisted"]], rec.privacy_status),
                      onchange: "this.form.submit()" %>
                <% end %>
              </td>
              <td style="white-space:nowrap;font-size:0.8rem">
                <% if rec.replay_url %>
                  <%= link_to "Guarda", rec.replay_url, target: "_blank", rel: "noopener" %>
                <% end %>
                <% if rec.team.entitlements.phone_download_enabled? && rec.ready? %>
                  · <%= link_to "MP4", public_replay_download_path(rec.stream_session_id) %>
                <% end %>
                <% ent = rec.team.entitlements %>
                <% if ent.premium_full? && ent.youtube_enabled? && rec.ready? && rec.youtube_video_id.blank? %>
                  · <%= button_to "YouTube", public_club_recording_publish_youtube_path(@club, rec), method: :post, class: "btn-link", style: "display:inline;padding:0;border:0;background:none;color:inherit;cursor:pointer;text-decoration:underline" %>
                <% elsif rec.youtube_watch_url %>
                  · <%= link_to "YT", rec.youtube_watch_url, target: "_blank", rel: "noopener" %>
                <% end %>
                · <%= button_to "Elimina", public_club_recording_path(@club, rec), method: :delete,
                    class: "btn-link", style: "display:inline;padding:0;border:0;background:none;color:#e53935;cursor:pointer",
                    form: { data: { turbo_confirm: "Eliminare definitivamente questo replay?" } } %>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>
    </div>
  <% else %>
    <p class="muted">Nessuna registrazione ancora. I replay compaiono al termine delle dirette Premium.</p>
  <% end %>
</div>
