<script lang="ts">
  import { sessions, loadSessions, loadSessionMessages, messages, currentSession } from '$lib/stores/chat'
  import { onMount } from 'svelte'

  onMount(() => {
    loadSessions()
  })
</script>

<div class="sessions-page">
  <h2>Sessions</h2>
  <div class="session-list">
    {#each $sessions as session (session.id)}
      <button
        class="session-card"
        class:active={$currentSession === session.id}
        onclick={() => loadSessionMessages(session.id)}
      >
        <div class="session-title">{session.title || 'Untitled'}</div>
        <div class="session-meta">
          {new Date(session.last_active_at).toLocaleString()}
        </div>
      </button>
    {:else}
      <div class="empty">No sessions yet. Start chatting with Nomos.</div>
    {/each}
  </div>
</div>

<style>
  .sessions-page {
    max-width: 720px;
    margin: 0 auto;
    padding: 2rem 1rem;
  }

  h2 {
    font-size: 1.125rem;
    font-weight: 600;
    margin-bottom: 1rem;
    color: var(--text);
  }

  .session-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
  }

  .session-card {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    color: var(--text);
    font-family: inherit;
    font-size: 0.875rem;
    text-align: left;
    transition: border-color 0.15s;
  }

  .session-card:hover {
    border-color: var(--accent-blue);
  }

  .session-card.active {
    border-color: var(--accent-blue);
    background: var(--bg-hover);
  }

  .session-title {
    font-weight: 500;
  }

  .session-meta {
    font-size: 0.75rem;
    color: var(--text-muted);
  }

  .empty {
    color: var(--text-muted);
    font-size: 0.875rem;
    padding: 2rem 0;
    text-align: center;
  }
</style>
