<script>
  import { get } from 'svelte/store';
  import { journals } from '../stores/journalStore.js';
  import TimelineToolbar from './TimelineToolbar.svelte';
  import TimelineCard from './TimelineCard.svelte';
  import JournalDetail from './JournalDetail.svelte';
  import JournalSummary from './JournalSummary.svelte';

  /** @type {import('../stores/journalStore.js').JournalEntry|null} */
  let selected = $state(null);

  let entries = $state(get(journals));
  $effect(() => {
    const unsub = journals.subscribe((v) => { entries = v; });
    return unsub;
  });

  let sortKey = $state('date-desc');

  let sortedEntries = $state(/** @type {typeof entries} */([]));
  $effect(() => {
    const key = sortKey;
    sortedEntries = [...entries].sort((a, b) => {
      if (key === 'date-asc')     return a.date.localeCompare(b.date);
      if (key === 'date-desc')    return b.date.localeCompare(a.date);
      if (key === 'country-asc')  return a.location.country.localeCompare(b.location.country) || b.date.localeCompare(a.date);
      if (key === 'country-desc') return b.location.country.localeCompare(a.location.country) || b.date.localeCompare(a.date);
      return 0;
    });
  });

  function getYear(iso) {
    return new Date(iso).getFullYear();
  }
</script>

<div class="journal-page">

  {#if selected}
    <div class="detail-scroll">
      <JournalDetail entry={selected} onBack={() => (selected = null)} />
    </div>
  {:else}
    <aside class="left-panel">
      <JournalSummary entries={sortedEntries} />
    </aside>

    <div class="right-panel">
      <div class="right-inner">
        <TimelineToolbar {sortKey} onSort={(k) => (sortKey = k)} />

        {#if sortedEntries.length === 0}
          <p class="empty">No journal entries yet.</p>
        {:else}
          <ol class="v-list">
            {#each sortedEntries as entry, i (entry.id)}
              {#if i === 0 || getYear(entry.date) !== getYear(sortedEntries[i - 1].date)}
                <li class="year-marker" aria-hidden="true">
                  <span class="year-label">{getYear(entry.date)}</span>
                </li>
              {/if}
              <TimelineCard {entry} onClick={() => (selected = entry)} />
            {/each}
          </ol>
        {/if}

        <footer class="page-footer">
          {sortedEntries.length} {sortedEntries.length === 1 ? 'entry' : 'entries'}
        </footer>
      </div>
    </div>
  {/if}

</div>

<style>
  .journal-page {
    flex: 1;
    display: flex;
    flex-direction: row;
    min-width: 0;
    height: 100%;
    overflow: hidden;
  }

  /* ── Left panel ── */
  .left-panel {
    width: 260px;
    flex-shrink: 0;
    overflow-y: auto;
    border-right: 1px solid var(--border);
    background: var(--bg-raised);
    padding: 40px 28px;
  }

  /* ── Right panel ── */
  .right-panel {
    flex: 1;
    min-width: 0;
    overflow-y: auto;
    background: var(--bg);
  }

  /* Inner container with max-width + generous side padding */
  .right-inner {
    max-width: 640px;
    margin: 0 auto;
    padding: 40px 48px 80px;
  }

  /* ── Responsive: narrow viewport ── */
  @media (max-width: 700px) {
    .journal-page { flex-direction: column; overflow-y: auto; overflow-x: hidden; }
    .left-panel {
      width: 100%;
      border-right: none;
      border-bottom: 1px solid var(--border);
      padding: 24px 20px;
    }
    .right-panel { overflow-y: unset; }
    .right-inner { padding: 24px 20px 60px; }
  }

  /* ── Detail view ── */
  .detail-scroll {
    flex: 1;
    overflow-y: auto;
    background: var(--bg);
  }

  /* ── Timeline list ── */
  .v-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 0;
  }

  /* Year marker */
  .year-marker {
    padding: 32px 0 14px;
  }
  .year-label {
    font-size: 13px;
    font-weight: 400;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--text);
    border-left: 2px solid var(--accent);
    padding-left: 10px;
  }

  .page-footer {
    margin-top: 56px;
    text-align: center;
    font-size: 11px;
    font-weight: 300;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--text-sub);
    padding-top: 24px;
    border-top: 1px solid var(--border);
  }

  .empty { text-align: center; color: var(--text-sub); padding: 80px 0; }
</style>
