@php
  /** @var \App\Models\Asporto $asporto */
  $ordine = $ordine ?? $asporto->ordine;
  $info = is_array($ordine?->info) ? $ordine->info : [];
  $showTelefono = $showTelefono ?? true;

  $cliente = $info['cliente'] ?? ($ordine->nome ?? '—');
  $telefono = trim((string) ($info['telefono'] ?? ''));
  $orario = $asporto->ora_ritiro
    ? \Illuminate\Support\Carbon::parse($asporto->ora_ritiro)->format('H:i')
    : trim((string) ($info['ora_ritiro'] ?? ''));
  $note = trim((string) ($ordine->note ?? ($info['note'] ?? ($info['note_ordine'] ?? ''))));
  $righe = $ordine?->righe_ordine ?? collect();
  $qta = (int) $righe->sum('quantita');
  $voci = (int) $righe->count();
  $prezzo = $ordine->prezzo ?? null;
  if ($prezzo === null || (float) $prezzo == 0.0) {
    $prezzo = $righe->sum('prezzo');
  }
  $ordinatoAlle = $ordine?->created_at
    ? \Illuminate\Support\Carbon::parse($ordine->created_at)->format('H:i')
    : '';

  $skipKeys = [
    'asporto',
    'cliente',
    'telefono',
    'ora_ritiro',
    'note',
    'note_ordine',
  ];

  $labelMap = [
    'tavolo' => 'Tavolo',
    'coperti' => 'Coperti',
    'email' => 'Email',
    'indirizzo' => 'Indirizzo',
    'citofono' => 'Citofono',
    'pagamento' => 'Pagamento',
    'metodo_pagamento' => 'Pagamento',
  ];

  $extraFacts = [];
  foreach ($info as $key => $value) {
    if (in_array((string) $key, $skipKeys, true)) {
      continue;
    }
    if ($value === null || $value === '' || $value === false) {
      continue;
    }
    if ($value === true) {
      $value = 'Sì';
    }
    if (is_array($value)) {
      $value = implode(', ', array_filter(array_map(function ($v) {
        if (is_bool($v)) {
          return $v ? 'Sì' : null;
        }
        if (is_scalar($v)) {
          return trim((string) $v) !== '' ? (string) $v : null;
        }

        return null;
      }, $value)));
      if ($value === '') {
        continue;
      }
    }

    $extraFacts[(string) $key] = is_scalar($value) ? (string) $value : json_encode($value);
  }

  if (! empty($asporto->posti)) {
    $extraFacts['posti'] = (string) $asporto->posti;
  }
@endphp

<dl class="asporto-pick-facts">
  <div class="asporto-pick-facts__row">
    <dt>Cliente</dt>
    <dd>{{ $cliente }}</dd>
  </div>

  @if($orario !== '')
    <div class="asporto-pick-facts__row">
      <dt>Orario ritiro</dt>
      <dd>{{ $orario }}</dd>
    </div>
  @endif

  @if($showTelefono && $telefono !== '')
    <div class="asporto-pick-facts__row">
      <dt>Telefono</dt>
      <dd>
        <a href="tel:{{ preg_replace('/\s+/', '', $telefono) }}" class="asporto-pick-cliente__tel">{{ $telefono }}</a>
      </dd>
    </div>
  @endif

  <div class="asporto-pick-facts__row">
    <dt>Articoli</dt>
    <dd>
      {{ $qta }}
      @if($voci > 0 && $voci !== $qta)
        <span class="text-muted fw-normal">({{ $voci }} {{ $voci === 1 ? 'voce' : 'voci' }})</span>
      @endif
    </dd>
  </div>

  @if($prezzo !== null && (float) $prezzo > 0)
    <div class="asporto-pick-facts__row">
      <dt>Totale</dt>
      <dd>{{ number_format((float) $prezzo, 2, ',', '.') }} €</dd>
    </div>
  @endif

  @if($ordinatoAlle !== '')
    <div class="asporto-pick-facts__row">
      <dt>Ordinato alle</dt>
      <dd>{{ $ordinatoAlle }}</dd>
    </div>
  @endif

  @foreach($extraFacts as $key => $value)
    <div class="asporto-pick-facts__row">
      <dt>{{ $labelMap[$key] ?? ucfirst(str_replace('_', ' ', $key)) }}</dt>
      <dd>{{ $value }}</dd>
    </div>
  @endforeach
</dl>

@if($note !== '')
  <div class="asporto-prep-panel__note">
    <i class="bx bx-error-circle me-1"></i>{{ $note }}
  </div>
@endif
