<!-- attachments.php -->
<div class="gateway-card">
  <p class="gateway-label" style="margin-bottom:1rem">Uploaded Files</p>

  <?php if (empty($attachments)): ?>
    <div style="text-align:center;padding:2.5rem 1rem">
      <div style="width:2.75rem;height:2.75rem;background:#f1f5f9;border-radius:9999px;display:flex;align-items:center;justify-content:center;margin:0 auto .75rem">
        <svg xmlns="http://www.w3.org/2000/svg" style="width:1.25rem;height:1.25rem;color:#94a3b8" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
          <path stroke-linecap="round" stroke-linejoin="round" d="M18.375 12.739l-7.693 7.693a4.5 4.5 0 01-6.364-6.364l10.94-10.94A3 3 0 1119.5 7.372L8.552 18.32m.009-.01l-.01.01m5.699-9.941l-7.81 7.81a1.5 1.5 0 002.112 2.13"/>
        </svg>
      </div>
      <p style="font-size:.875rem;color:#94a3b8">No files uploaded yet.</p>
    </div>
  <?php else: ?>
    <?php
      usort($attachments, fn($a, $b) => strtotime($b['created_at'] ?? '0') <=> strtotime($a['created_at'] ?? '0'));
      $ext_colors = [
        'pdf'  => ['#fee2e2','#dc2626'],
        'doc'  => ['#dbeafe','#1d4ed8'], 'docx' => ['#dbeafe','#1d4ed8'],
        'xls'  => ['#dcfce7','#15803d'], 'xlsx' => ['#dcfce7','#15803d'],
        'jpg'  => ['#fce7f3','#be185d'], 'jpeg' => ['#fce7f3','#be185d'],
        'png'  => ['#f3e8ff','#7e22ce'], 'gif'  => ['#f3e8ff','#7e22ce'],
        'zip'  => ['#fef3c7','#b45309'], 'mp4'  => ['#e0f2fe','#0369a1'],
      ];
    ?>
    <div style="display:flex;flex-direction:column;gap:.5rem">
      <?php foreach ($attachments as $file):
        $name       = $file['name'] ?? 'Unnamed';
        $size       = isset($file['size']) ? size_format($file['size'], 1) : '';
        $ext        = strtolower(pathinfo($name, PATHINFO_EXTENSION));
        $is_image   = in_array($ext, ['jpg','jpeg','png','gif','webp'], true);
        $thumb      = $is_image ? ($file['permalink'] ?? null) : null;
        $uploader   = trim((string)($file['created_by']['name'] ?? ''));
        if ($uploader === '' || strtolower($uploader) === 'unknown') {
          if (!empty($submitter_name)) {
            $uploader = (string)$submitter_name;
          } elseif (!empty($submitter_email)) {
            $uploader = (string)$submitter_email;
          } else {
            $uploader = 'Client';
          }
        }
        $up_at      = !empty($file['created_at']) ? date('M j, Y \a\t g:i a', strtotime($file['created_at'])) : '';
        [$ic_bg, $ic_text] = $ext_colors[$ext] ?? ['#f1f5f9','#64748b'];
      ?>
        <a href="<?= esc_url($file['download_url'] ?? '#') ?>" target="_blank" class="gateway-file-card">
          <?php if ($thumb): ?>
            <img src="<?= esc_attr($thumb) ?>" class="object-cover rounded-md" style="width:2.75rem;height:2.75rem;flex-shrink:0" alt="">
          <?php else: ?>
            <div class="gateway-file-icon" style="background:<?= $ic_bg ?>;color:<?= $ic_text ?>">
              <?= esc_html(strtoupper($ext ?: '—')) ?>
            </div>
          <?php endif; ?>

          <div style="flex:1;min-width:0">
            <p style="font-size:.875rem;font-weight:600;color:#1e293b;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;margin:0"><?= esc_html($name) ?></p>
            <p style="font-size:.75rem;color:#94a3b8;margin:.2rem 0 0">
              <?= $size ? esc_html($size) . ' · ' : '' ?>
              <?= $up_at ? 'Uploaded ' . esc_html($up_at) . ' by ' . esc_html($uploader) : esc_html($uploader) ?>
            </p>
          </div>

          <svg xmlns="http://www.w3.org/2000/svg" style="width:1rem;height:1rem;color:#94a3b8;flex-shrink:0" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
            <path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5M16.5 12L12 16.5m0 0L7.5 12m4.5 4.5V3"/>
          </svg>
        </a>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
</div>
