<% content_for :title do %>Dirette in corso — Match Live TV<% end %>

<div class="wrap">
<h1>Dirette in corso</h1>
<p class="results-hint">
  Cerca per nome squadra o avversario, poi apri la diretta per guardare lo stream.
</p>

<%= form_with url: public_live_index_path, method: :get, local: true, class: "search-form" do %>
  <input
    type="search"
    name="q"
    value="<%= @query %>"
    placeholder="Es. Tigers, Eagles, nome squadra…"
    aria-label="Cerca squadra"
    autocomplete="off"
  />
  <button type="submit" class="btn btn-primary">Cerca</button>
  <% if @query.present? %>
    <%= link_to "Azzera", public_live_index_path, class: "btn btn-secondary" %>
  <% end %>
<% end %>

<% if @query.present? %>
  <p class="results-hint">
    Risultati per «<%= h @query %>»: <%= @sessions.size %> diretta<%= @sessions.size == 1 ? "" : "e" %>
  </p>
<% end %>

<% if @sessions.any? %>
  <div class="live-grid">
    <% @sessions.each do |session| %>
      <% match = session.match %>
      <% on_air = @online_paths.include?(session.mediamtx_path_name) %>
      <article class="live-card">
        <h3><%= match.team.name %> vs <%= match.opponent_name %></h3>
        <p class="meta">
          <% if match.location.present? %><%= match.location %> · <% end %>
          <%= session.platform == "matchlivetv" ? "Match Live TV" : session.platform.capitalize %>
        </p>
        <% if session.score_state %>
          <p class="card-score">
            <span class="card-sets"><%= live_score_sets_label(session.score_state) %></span>
            <% if live_score_partials_label(session.score_state).present? %>
              <span class="card-partials">Parziali: <%= live_score_partials_label(session.score_state) %></span>
            <% end %>
            <span class="card-points"><%= session.score_state.home_points %> - <%= session.score_state.away_points %></span>
          </p>
        <% end %>
        <div class="badges">
          <% if on_air %>
            <span class="badge badge-on-air">In onda</span>
          <% elsif session.status == "live" %>
            <span class="badge badge-live">Live</span>
          <% elsif session.status == "reconnecting" %>
            <span class="badge badge-connecting">Riconnessione</span>
          <% else %>
            <span class="badge badge-wait">In avvio</span>
          <% end %>
        </div>
        <%= link_to "Guarda diretta →", public_live_path(session), class: "btn-watch" %>
      </article>
    <% end %>
  </div>
<% else %>
  <div class="empty-state">
    <% if @query.present? %>
      <p>Nessuna diretta attiva per «<%= h @query %>».</p>
      <p>Prova un altro nome squadra o <%= link_to "mostra tutte le dirette", public_live_index_path %>.</p>
    <% else %>
      <p>Al momento non ci sono dirette attive.</p>
      <p>Quando una squadra avvia lo streaming dall’app, la partita comparirà qui.</p>
    <% end %>
  </div>
<% end %>
</div>
