<script>
  /** @type {{ entry: import('../stores/journalStore.js').JournalEntry, onClick: () => void }} */
  let { entry, onClick } = $props();

  /** Convert country name to flag emoji via ISO 3166-1 alpha-2 */
  const countryCodeMap = {
    'Japan': 'JP', 'France': 'FR', 'Spain': 'ES', 'USA': 'US',
    'Thailand': 'TH', 'Germany': 'DE', 'Italy': 'IT', 'UK': 'GB',
    'Australia': 'AU', 'Canada': 'CA', 'China': 'CN', 'India': 'IN',
    'Brazil': 'BR', 'Mexico': 'MX', 'Portugal': 'PT', 'Netherlands': 'NL',
    'Greece': 'GR', 'Turkey': 'TR', 'Vietnam': 'VN', 'Indonesia': 'ID',
    'Malaysia': 'MY', 'Singapore': 'SG', 'South Korea': 'KR', 'Taiwan': 'TW',
    'New Zealand': 'NZ', 'Argentina': 'AR', 'Chile': 'CL', 'Peru': 'PE',
    'Morocco': 'MA', 'Egypt': 'EG', 'Kenya': 'KE', 'South Africa': 'ZA',
    'Sweden': 'SE', 'Norway': 'NO', 'Denmark': 'DK', 'Finland': 'FI',
    'Switzerland': 'CH', 'Austria': 'AT', 'Belgium': 'BE', 'Poland': 'PL',
    'Czech Republic': 'CZ', 'Hungary': 'HU', 'Croatia': 'HR',
  };

  function flagEmoji(country) {
    const code = countryCodeMap[country];
    if (!code) return '';
    return [...code].map(c => String.fromCodePoint(0x1F1E6 - 65 + c.charCodeAt(0))).join('');
  }

  function formatDate(/** @type {string} */ iso) {
    return new Date(iso).toLocaleDateString('en-US', {
      month: 'short', day: 'numeric', year: 'numeric',
    });
  }

  let mainPhoto   = $derived(entry.photos[0] ?? null);
  let thumbPhotos = $derived(entry.photos.slice(1, 4));
  let extraCount  = $derived(entry.photos.length > 4 ? entry.photos.length - 4 : 0);
</script>

<li class="v-item">
  <div class="v-dot" aria-hidden="true"></div>

  <div class="v-content">
    <!-- Country above card -->
    <div class="above-card">
      <span class="flag">{flagEmoji(entry.location.country)}</span>
      <span class="country-name">{entry.location.country}</span>
    </div>

    <!-- Card -->
    <div class="entry-card" role="button" tabindex="0"
      onclick={onClick}
      onkeydown={(e) => e.key === 'Enter' && onClick()}>

      <!-- Trip badge — top-right of card, outside photo -->
      <span class="trip-badge trip-badge--{entry.tripType}">
        {entry.tripType === 'solo' ? 'Solo' : 'Friends'}
      </span>

      <!-- Photos -->
      <div class="photo-grid" class:has-thumbs={thumbPhotos.length > 0}>
        <div class="photo-main">
          {#if mainPhoto}
            <img src={mainPhoto} alt="" loading="lazy"
              onerror={(e) => {
                e.currentTarget.style.display = 'none';
                e.currentTarget.nextElementSibling.style.display = 'flex';
              }} />
            <div class="photo-fallback" style="display:none">
              <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.2">
                <rect x="3" y="3" width="18" height="18" rx="3"/>
                <circle cx="8.5" cy="8.5" r="1.5"/>
                <path d="M21 15l-5-5L5 21"/>
              </svg>
            </div>
          {:else}
            <div class="photo-fallback">
              <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.2">
                <rect x="3" y="3" width="18" height="18" rx="3"/>
                <circle cx="8.5" cy="8.5" r="1.5"/>
                <path d="M21 15l-5-5L5 21"/>
              </svg>
            </div>
          {/if}
        </div>

        {#if thumbPhotos.length > 0}
          <div class="photo-thumbs">
            {#each thumbPhotos as photo, i}
              <div class="photo-thumb">
                <img src={photo} alt="" loading="lazy"
                  onerror={(e) => {
                    e.currentTarget.style.display = 'none';
                    e.currentTarget.nextElementSibling.style.display = 'flex';
                  }} />
                <div class="thumb-fallback" style="display:none">
                  <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.2">
                    <rect x="3" y="3" width="18" height="18" rx="3"/>
                    <circle cx="8.5" cy="8.5" r="1.5"/>
                    <path d="M21 15l-5-5L5 21"/>
                  </svg>
                </div>
                {#if i === 2 && extraCount > 0}
                  <div class="extra-overlay">+{extraCount}</div>
                {/if}
              </div>
            {/each}
          </div>
        {/if}
      </div>

      <!-- Info bar -->
      <div class="card-info">
        <span class="city">{entry.location.city}</span>
        <div class="meta">
          <span>{formatDate(entry.date)}</span>
          <span class="dot-sep">·</span>
          <span>{entry.days} {entry.days === 1 ? 'day' : 'days'}</span>
        </div>
      </div>

    </div>
  </div>
</li>

<style>
  /* ── Timeline row ── */
  .v-item {
    display: flex;
    gap: 14px;
    align-items: flex-start;
    padding-bottom: 28px;
    position: relative;
  }
  .v-item:last-child { padding-bottom: 0; }
  .v-item:not(:last-child)::after {
    content: '';
    position: absolute;
    left: 4px;
    top: 34px;
    bottom: 0;
    width: 1px;
    background: var(--border);
  }

  .v-dot {
    flex-shrink: 0;
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--bg);
    border: 1.5px solid var(--accent);
    margin-top: 6px;
    z-index: 1;
  }

  /* ── Content column (above-card + card) ── */
  .v-content {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
  }

  /* ── Country above card ── */
  .above-card {
    display: flex;
    align-items: center;
    gap: 6px;
    padding-left: 2px;
  }
  .flag { font-size: 16px; line-height: 1; }
  .country-name {
    font-size: 15px;
    font-weight: 400;
    color: var(--text-h);
    letter-spacing: -0.2px;
  }

  /* ── Card ── */
  .entry-card {
    position: relative;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--bg-raised);
    box-shadow: 0 1px 4px rgba(0,0,0,0.05);
    cursor: pointer;
    overflow: hidden;
    transition: box-shadow 0.2s, transform 0.15s;
    container-type: inline-size;
    container-name: card;
  }
  .entry-card:hover {
    box-shadow: 0 6px 20px rgba(0,0,0,0.09);
    transform: translateY(-2px);
  }

  /* ── Trip badge — absolute top-right of card ── */
  .trip-badge {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 2;
    font-size: 11px;
    font-weight: 300;
    padding: 3px 10px;
    border-radius: 20px;
    letter-spacing: 0.04em;
    backdrop-filter: blur(6px);
  }
  .trip-badge--solo    { background: rgba(245,158,11,0.85); color: #fff; }
  .trip-badge--friends { background: rgba(124,58,237,0.85); color: #fff; }

  /* ── Photo grid — fixed height, always consistent ── */
  .photo-grid {
    display: grid;
    grid-template-columns: 1fr;
    height: 180px;
    background: var(--bg-subtle);
  }
  .photo-grid.has-thumbs {
    grid-template-columns: 2fr 1fr;
    gap: 2px;
  }

  @container card (max-width: 300px) {
    .photo-grid.has-thumbs { grid-template-columns: 1fr; }
    .photo-thumbs { display: none; }
  }

  /* ── Main photo ── */
  .photo-main {
    overflow: hidden;
    height: 100%;
    background: var(--bg-subtle);
  }
  .photo-main img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
  }
  .photo-fallback {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-sub);
  }

  /* ── Thumbs ── */
  .photo-thumbs {
    display: flex;
    flex-direction: column;
    gap: 2px;
  }
  .photo-thumb {
    position: relative;
    flex: 1;
    overflow: hidden;
    min-height: 0;
    background: var(--bg-subtle);
  }
  .photo-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
  }
  .thumb-fallback {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-sub);
    background: var(--bg-subtle);
  }
  .extra-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: 400;
    color: #fff;
  }

  /* ── Info bar ── */
  .card-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    background: var(--bg);
    border-top: 1px solid var(--border);
    gap: 8px;
    min-height: 44px;
  }
  .city {
    font-size: 13px;
    font-weight: 300;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .meta {
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 12px;
    font-weight: 300;
    color: var(--text-sub);
    white-space: nowrap;
    flex-shrink: 0;
  }
  .dot-sep { color: var(--border-bright); }
</style>
