@php
  use Illuminate\Support\Facades\Auth;

  $searchText = implode(' ', array_filter([
    $device['nome'] ?? '',
    $device['attivita_nome'] ?? '',
    $device['ubicazione'] ?? '',
    $device['tipo'] ?? '',
    (string) ($device['id'] ?? ''),
  ]));
  $attivitaId = $device['attivita_id'] ?? null;
  $attivitaLabel = $device['attivita_nome'] ?? 'Senza attività';
  $isPv = $device['is_punto_vendita'] ?? false;
  $deviceId = (int) ($device['id'] ?? 0);
  $tipoKey = $device['tipo'] ?? '_altro';
  $tipoLabel = $tipoMeta['label'] ?? ucfirst($tipoKey);
  $tipoIcon = $tipoMeta['icon'] ?? 'bx bx-devices';
  $isAttivo = ! empty($device['is_attivo']);
  $isAbbinato = ! empty($device['has_binding']) || ! empty($device['occupato']);
  $canViewPv = $isPv && Auth::user()->can('view-punto_vendita');
  $canEditPv = $isPv && ($puoModificarePv ?? false);
  $canDissociarePv = $isPv && (
    Auth::user()->hasRole('superadmin')
    || Auth::user()->hasRole('amministratore')
    || Auth::user()->hasRole('administrator')
  );
  $showDissocia = $canDissociarePv && ! empty($device['has_binding']);
  $entraUrl = $canViewPv
    ? route('punto-vendita.show', ['punto_vendita_id' => $deviceId])
    : null;
  $vaiAlPvUrl = $canViewPv
    ? route('punto-vendita.dettagli-punto-vendita', ['punto_vendita_id' => $deviceId])
    : null;
  $editUrl = $canEditPv
    ? route('punto-vendita.edit', ['punto_vendita_id' => $deviceId])
    : null;
  $hasActions = $entraUrl || $vaiAlPvUrl || $editUrl || $showDissocia;
@endphp

<div class="col-sm-6 col-lg-4 col-xl-3 md-device-col">
  <div
    class="card md-device-card h-100"
    data-tipo="{{ $tipoKey }}"
    data-attivita-id="{{ $attivitaId ?? '0' }}"
    data-attivo="{{ $isAttivo ? '1' : '0' }}"
    data-search="{{ $searchText }}"
    @if($isPv) data-md-pv-id="{{ $deviceId }}" @endif
  >
    <div class="card-body d-flex flex-column">
      <div class="d-flex align-items-center gap-3 mb-3">
        <div class="md-device-card__icon">
          <i class="{{ $tipoIcon }}"></i>
        </div>
        <div class="min-w-0">
          <h6 class="mb-0 text-truncate fw-semibold" title="{{ $device['nome'] }}">{{ $device['nome'] }}</h6>
          <small class="text-muted">{{ $tipoLabel }} · #{{ $deviceId }}</small>
        </div>
      </div>

      <div class="d-flex flex-wrap gap-2 mb-3">
        @if($isAttivo)
          <span class="badge bg-label-success">Attivo</span>
        @else
          <span class="badge bg-label-danger">Non attivo</span>
        @endif
        @if($isPv)
          @if($isAbbinato)
            <span class="badge bg-label-info md-pv-binding-badge">Abbinato</span>
          @else
            <span class="badge bg-label-secondary md-pv-binding-badge">Libero</span>
          @endif
        @endif
      </div>

      @if($device['ubicazione'] || $attivitaLabel)
        <p class="small text-muted mb-3">
          @if($device['ubicazione'])
            <span class="d-block text-truncate" title="{{ $device['ubicazione'] }}">
              <i class="bx bx-map-pin me-1"></i>{{ $device['ubicazione'] }}
            </span>
          @endif
          <span class="d-block text-truncate" title="{{ $attivitaLabel }}">
            <i class="bx bx-buildings me-1"></i>{{ $attivitaLabel }}
          </span>
        </p>
      @endif

      @if($hasActions)
        <div class="md-device-card__actions d-grid gap-2 mt-auto pt-2 border-top">
          @if($entraUrl || $vaiAlPvUrl)
            <div class="d-flex gap-2">
              @if($entraUrl)
                <a href="{{ $entraUrl }}" class="btn btn-sm btn-primary">
                  <i class="bx bx-log-in me-1"></i> Entra
                </a>
              @endif
              @if($vaiAlPvUrl)
                <a href="{{ $vaiAlPvUrl }}" class="btn btn-sm btn-outline-primary">
                  <i class="bx bx-store me-1"></i> Vai al PV
                </a>
              @endif
            </div>
          @endif
          @if($editUrl || $showDissocia)
            <div class="d-flex gap-2">
              @if($editUrl)
                <a href="{{ $editUrl }}" class="btn btn-sm btn-label-primary">
                  <i class="bx bx-edit-alt me-1"></i> Gestisci
                </a>
              @endif
              @if($showDissocia)
                <button
                  type="button"
                  class="btn btn-sm btn-outline-danger md-dissocia-pv"
                  data-pv-id="{{ $deviceId }}">
                  <i class="bx bx-unlink me-1"></i> Dissocia
                </button>
              @endif
            </div>
          @endif
        </div>
      @endif
    </div>
  </div>
</div>
