<script>
  /** @type {{ entries: import('../shared/types.js').JournalEntry[], onClick: () => void }} */
  let { entries, onClick } = $props();

  const continentMap = {
    'Japan':'Asia','South Korea':'Asia','China':'Asia','Thailand':'Asia','Vietnam':'Asia',
    'Indonesia':'Asia','Malaysia':'Asia','Singapore':'Asia','India':'Asia','Taiwan':'Asia',
    'Philippines':'Asia','Cambodia':'Asia','Nepal':'Asia',
    'France':'Europe','Spain':'Europe','Italy':'Europe','Germany':'Europe','UK':'Europe',
    'Netherlands':'Europe','Portugal':'Europe','Greece':'Europe','Sweden':'Europe',
    'Norway':'Europe','Denmark':'Europe','Finland':'Europe','Switzerland':'Europe',
    'Austria':'Europe','Belgium':'Europe','Poland':'Europe','Czech Republic':'Europe',
    'Hungary':'Europe','Croatia':'Europe','Turkey':'Europe',
    'USA':'N. America','Canada':'N. America','Mexico':'N. America',
    'Brazil':'S. America','Argentina':'S. America','Chile':'S. America','Peru':'S. America',
    'Australia':'Oceania','New Zealand':'Oceania',
    'Morocco':'Africa','Egypt':'Africa','Kenya':'Africa','South Africa':'Africa',
  };

  const continentColors = {
    'Asia':'#f87171','Europe':'#818cf8','N. America':'#4ade80',
    'S. America':'#fbbf24','Africa':'#fb923c','Oceania':'#c084fc',
  };

  let stats = $derived.by(() => {
    const totalDays = entries.reduce((s, e) => s + e.days, 0);
    const countries = [...new Set(entries.map(e => e.location.country))];
    const contDays = {};
    for (const e of entries) {
      const c = continentMap[e.location.country] ?? 'Other';
      contDays[c] = (contDays[c] ?? 0) + e.days;
    }
    const top = Object.entries(contDays).sort((a, b) => b[1] - a[1]);
    return { totalDays, countries, contDays, top, trips: entries.length };
  });
</script>

<button class="preview-card" onclick={onClick} aria-label="Share your journey">

  <div class="pc-bg"></div>
  <div class="pc-grid-pattern"></div>

  <div class="pc-header">
    <span class="pc-brand">MAP JOURNAL</span>
  </div>

  <div class="pc-hero">
    <span class="pc-num">{stats.totalDays}</span>
    <span class="pc-label">days traveled</span>
  </div>

  <div class="pc-row">
    <div class="pc-stat">
      <span class="pc-stat-num">{stats.countries.length}</span>
      <span class="pc-stat-label">countries</span>
    </div>
    <div class="pc-stat">
      <span class="pc-stat-num">{stats.trips}</span>
      <span class="pc-stat-label">trips</span>
    </div>
    <div class="pc-stat">
      <span class="pc-stat-num">{Object.keys(stats.contDays).length}</span>
      <span class="pc-stat-label">continents</span>
    </div>
  </div>

  {#if stats.top.length > 0}
    <div class="pc-bar-wrap">
      <div class="pc-bar">
        {#each stats.top as [cont, days]}
          <div class="pc-seg" style="flex:{days}; background:{continentColors[cont] ?? '#818cf8'}"></div>
        {/each}
      </div>
      <div class="pc-bar-labels">
        {#each stats.top.slice(0,3) as [cont, days]}
          <span class="pc-bar-label" style="color:{continentColors[cont] ?? '#818cf8'}">{cont}</span>
        {/each}
      </div>
    </div>
  {/if}

  <svg class="pc-share-icon-corner" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
    <circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/>
    <path d="m8.59 13.51 6.83 3.98M15.41 6.51l-6.82 3.98"/>
  </svg>

</button>

<style>
  .preview-card {
    position: sticky;
    top: 40px;
    width: 100%;
    background: #1a1630;
    border-radius: 12px;
    overflow: hidden;
    color: #fff;
    cursor: pointer;
    border: 1px solid rgba(255,255,255,0.06);
    text-align: left;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    transition: transform 0.15s, box-shadow 0.15s;
    box-shadow: 0 2px 12px rgba(0,0,0,0.08);
    font-family: var(--sans);
  }
  .preview-card:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 20px rgba(124,58,237,0.12);
  }

  .pc-bg {
    position: absolute;
    inset: 0;
    background:
      radial-gradient(ellipse 80% 60% at 90% 0%, rgba(124,58,237,0.2) 0%, transparent 60%),
      radial-gradient(ellipse 60% 60% at 0% 100%, rgba(99,102,241,0.1) 0%, transparent 60%);
    pointer-events: none;
  }
  .pc-grid-pattern {
    position: absolute;
    inset: 0;
    background-image:
      linear-gradient(rgba(255,255,255,0.025) 1px, transparent 1px),
      linear-gradient(90deg, rgba(255,255,255,0.025) 1px, transparent 1px);
    background-size: 24px 24px;
    pointer-events: none;
  }

  .pc-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
    z-index: 1;
  }
  .pc-brand {
    font-size: 8px;
    font-weight: 500;
    letter-spacing: 0.2em;
    color: #a5b4fc;
  }
  .pc-share-icon-corner {
    position: absolute;
    bottom: 14px;
    right: 14px;
    color: #a5b4fc;
    z-index: 1;
  }

  .pc-hero {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    gap: 2px;
  }
  .pc-num {
    font-size: 40px;
    font-weight: 400;
    line-height: 1;
    letter-spacing: -1.5px;
    color: #fff;
  }
  .pc-label {
    font-size: 11px;
    font-weight: 300;
    color: #a5b4fc;
    letter-spacing: 0.04em;
  }

  .pc-row {
    display: flex;
    gap: 0;
    position: relative;
    z-index: 1;
  }
  .pc-stat {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding-right: 12px;
    border-right: 1px solid rgba(255,255,255,0.08);
  }
  .pc-stat:last-child { border-right: none; padding-right: 0; padding-left: 12px; }
  .pc-stat:not(:first-child):not(:last-child) { padding-left: 12px; }
  .pc-stat-num {
    font-size: 16px;
    font-weight: 400;
    color: #fff;
    letter-spacing: -0.5px;
    line-height: 1;
  }
  .pc-stat-label {
    font-size: 9px;
    font-weight: 300;
    color: rgba(255,255,255,0.4);
    text-transform: uppercase;
    letter-spacing: 0.1em;
  }

  .pc-bar-wrap {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    gap: 6px;
  }
  .pc-bar {
    display: flex;
    height: 4px;
    border-radius: 2px;
    overflow: hidden;
    gap: 2px;
  }
  .pc-seg { border-radius: 2px; min-width: 3px; }
  .pc-bar-labels {
    display: flex;
    gap: 10px;
  }
  .pc-bar-label {
    font-size: 9px;
    font-weight: 300;
    letter-spacing: 0.04em;
  }

  .pc-cta {
    position: relative;
    z-index: 1;
    font-size: 11px;
    font-weight: 400;
    color: #a5b4fc;
    letter-spacing: 0.04em;
    padding-top: 4px;
    border-top: 1px solid rgba(255,255,255,0.08);
    text-align: right;
  }
</style>
