@php
  $andamento = collect($andamentoMensile ?? []);
  $ricavi = collect($ricaviRows ?? []);
  $costi = collect($costiRows ?? []);

  $lastMonth = $andamento->last();
  $prevMonth = $andamento->slice(-2, 1)->first();

  $lastSaldo = (float) ($lastMonth['saldo'] ?? 0);
  $prevSaldo = (float) ($prevMonth['saldo'] ?? 0);
  $saldoDelta = $lastSaldo - $prevSaldo;

  $saldoTrendClass = $saldoDelta >= 0 ? 'text-success' : 'text-danger';
  $saldoTrendIcon = $saldoDelta >= 0 ? 'bx-chevron-up' : 'bx-chevron-down';

  $topRicavo = $ricavi->first();
  $topCosto = $costi->first();
  $bestMonth = $andamento->sortByDesc('saldo')->first();
@endphp

<div class="tab-pane fade show active" id="bilancio-tab-sintesi" role="tabpanel">
  <div class="row g-3 mb-3">
    <div class="col-12 col-md-6 col-xl-3">
      <div class="card card-border-shadow-primary h-100">
        <div class="card-body">
          <div class="d-flex align-items-center justify-content-between">
            <div class="content-left">
              <span class="text-heading">Saldo annuale</span>
              <h5 class="mb-0 mt-1 {{ ($saldoTotale ?? 0) >= 0 ? 'text-success' : 'text-danger' }}">
                € {{ number_format((float)($saldoTotale ?? 0), 2, ',', '.') }}
              </h5>
              <small class="{{ $saldoTrendClass }}">
                <i class="bx {{ $saldoTrendIcon }}"></i>
                {{ number_format(abs($saldoDelta), 2, ',', '.') }} vs mese precedente
              </small>
            </div>
            <span class="avatar-initial rounded bg-label-primary">
              <i class="bx bx-line-chart bx-lg"></i>
            </span>
          </div>
        </div>
      </div>
    </div>

    <div class="col-12 col-md-6 col-xl-3">
      <div class="card card-border-shadow-success h-100">
        <div class="card-body">
          <div class="d-flex align-items-center justify-content-between">
            <div class="content-left">
              <span class="text-heading">Ricavi totali</span>
              <h5 class="mb-0 mt-1 text-success">
                € {{ number_format((float)($ricaviTotali ?? 0), 2, ',', '.') }}
              </h5>
              <small>{{ (int)($movimentiCount ?? 0) }} movimenti registrati</small>
            </div>
            <span class="avatar-initial rounded bg-label-success">
              <i class="bx bx-trending-up bx-lg"></i>
            </span>
          </div>
        </div>
      </div>
    </div>

    <div class="col-12 col-md-6 col-xl-3">
      <div class="card card-border-shadow-danger h-100">
        <div class="card-body">
          <div class="d-flex align-items-center justify-content-between">
            <div class="content-left">
              <span class="text-heading">Costi totali</span>
              <h5 class="mb-0 mt-1 text-danger">
                € {{ number_format((float)($costiTotali ?? 0), 2, ',', '.') }}
              </h5>
              <small>Top costo: {{ $topCosto['voce'] ?? 'N/D' }}</small>
            </div>
            <span class="avatar-initial rounded bg-label-danger">
              <i class="bx bx-trending-down bx-lg"></i>
            </span>
          </div>
        </div>
      </div>
    </div>

    <div class="col-12 col-md-6 col-xl-3">
      <div class="card card-border-shadow-info h-100">
        <div class="card-body">
          <div class="d-flex align-items-center justify-content-between">
            <div class="content-left">
              <span class="text-heading">Insight rapido</span>
              <h6 class="mb-0 mt-1">
                {{ !empty($bestMonth['mese']) ? 'Mese migliore: ' . $bestMonth['mese'] : 'Mese migliore non disponibile' }}
              </h6>
              <small>Top ricavo: {{ $topRicavo['voce'] ?? 'N/D' }}</small>
            </div>
            <span class="avatar-initial rounded bg-label-info">
              <i class="bx bx-bulb bx-lg"></i>
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>

  <h6 class="mb-2">Tabella Bilancio</h6>
  <div class="table-responsive">
    <table class="table table-sm table-bordered align-middle mb-0">
      <thead>
        <tr>
          <th class="text-end">Totale ricavi</th>
          <th class="text-center" style="width: 40px;">-</th>
          <th class="text-end">Totale costi</th>
          <th class="text-center" style="width: 40px;">=</th>
          <th class="text-end">Saldo totale</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="text-end fw-semibold">€ {{ number_format((float)($ricaviTotali ?? 0), 2, ',', '.') }}</td>
          <td class="text-center fw-semibold">-</td>
          <td class="text-end fw-semibold">€ {{ number_format((float)($costiTotali ?? 0), 2, ',', '.') }}</td>
          <td class="text-center fw-semibold">=</td>
          <td class="text-end fw-bold {{ (($saldoTotale ?? 0) >= 0) ? 'text-primary' : 'text-warning' }}">
            € {{ number_format((float)($saldoTotale ?? 0), 2, ',', '.') }}
          </td>
        </tr>
      </tbody>
    </table>
  </div>
  <div id="bilancio-saldo-col-chart" class="mt-3" style="min-height: 280px;"></div>
  <p class="text-muted small mt-2 mb-0">
    Come leggere: le colonne mostrano ricavi e costi per mese, la linea mostra il saldo (ricavi - costi). Se la linea sale, il margine migliora.
  </p>
</div>
