---
import { getLatestPublishedDispatches } from '../lib/db';
import {
  dispatchSlug, dispatchKindLabel, splitExcerpt, relativeTime,
} from '../lib/format';

const [latest] = getLatestPublishedDispatches(1);

const [p1, p2] = latest
  ? splitExcerpt(latest.excerpt || latest.body)
  : ['', null];

// Mark p2 with an ellipsis when the source extends beyond what we used —
// i.e. the body is longer than excerpt + paragraph break.
const sourceLen = latest ? (latest.excerpt || latest.body).trim().length : 0;
const usedLen   = p1.length + (p2 ? p2.length + 2 : 0);
const truncated = sourceLen > usedLen + 4;

const authorFirstName = latest ? latest.author_name.split(' ')[0] : '';
const authorInitial   = authorFirstName ? authorFirstName[0].toUpperCase() : '';
const authorRole      = latest?.author_title ?? 'team';
---
{latest && (
  <div class="rr-dispatch">

    <div class="rr-dispatch-meta">
      <div class="rr-dispatch-meta-left">
        <span class="rr-dispatch-eyebrow">
          Latest dispatch · {relativeTime(latest.published_at ?? latest.created_at)}
        </span>
        <span class:list={['rr-dispatch-kind', `rr-dispatch-kind-${latest.kind}`]}>
          {dispatchKindLabel(latest.kind)}
        </span>
      </div>
      <a class="rr-dispatch-all" href="/dispatches">All dispatches →</a>
    </div>

    <h2 class="rr-dispatch-title">{latest.title}</h2>

    <div class="rr-dispatch-body">
      <div class="rr-dispatch-text">
        <p class="rr-dispatch-p1">{p1}</p>
        {p2 && (
          <p class="rr-dispatch-p2">{p2}{truncated ? '…' : ''}</p>
        )}
      </div>

      <div class="rr-dispatch-author">
        <div class="rr-dispatch-author-row">
          <div class="rr-dispatch-author-text">
            <p class="rr-dispatch-author-name">{authorFirstName}</p>
            <p class="rr-dispatch-author-role">{authorRole}</p>
          </div>
          <div class="rr-dispatch-author-avatar">{authorInitial}</div>
        </div>
        <a class="rr-dispatch-cta" href={`/dispatches/${dispatchSlug(latest)}`}>
          Read full dispatch →
        </a>
      </div>
    </div>

  </div>
)}

<style>
  .rr-dispatch {
    background: var(--surface-card);
    border: 0.5px solid var(--surface-card-border);
    border-radius: 14px;
    padding: 36px 40px;
  }

  .rr-dispatch-meta {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 24px;
    align-items: baseline;
    margin-bottom: 22px;
  }
  .rr-dispatch-meta-left {
    display: flex;
    align-items: center;
    gap: 14px;
    flex-wrap: wrap;
  }
  .rr-dispatch-eyebrow {
    font-family: var(--font-sans);
    font-size: 10px;
    letter-spacing: 1.6px;
    text-transform: uppercase;
    color: var(--on-surface-variant);
  }
  .rr-dispatch-kind {
    font-family: var(--font-sans);
    font-size: 9px;
    letter-spacing: 0.8px;
    padding: 2px 8px;
    border-radius: 3px;
    text-transform: uppercase;
    font-weight: 500;
  }
  .rr-dispatch-kind-decision          { background: rgba(44,58,82,0.10);   color: #2c3a52; }
  .rr-dispatch-kind-update            { background: rgba(109,140,124,0.12);color: #6d8c7c; }
  .rr-dispatch-kind-behind_the_scenes { background: rgba(120,95,83,0.12);  color: #785f53; }
  .rr-dispatch-kind-note              { background: rgba(185,107,88,0.10); color: #b96b58; }

  .rr-dispatch-all {
    font-family: var(--font-sans);
    font-size: 11px;
    letter-spacing: 1px;
    color: var(--on-surface-variant);
    text-transform: uppercase;
    text-decoration: none;
    border-bottom: none;
  }
  .rr-dispatch-all:hover { color: var(--on-surface); border-bottom: none; }

  .rr-dispatch-title {
    font-family: var(--font-serif);
    font-size: 30px;
    line-height: 1.2;
    color: var(--on-surface);
    margin: 0 0 22px;
    max-width: 720px;
  }

  .rr-dispatch-body {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 40px;
    align-items: end;
  }
  .rr-dispatch-text { max-width: 720px; }
  .rr-dispatch-p1 {
    font-size: 14px;
    line-height: 1.7;
    color: var(--on-surface);
    margin: 0 0 10px;
  }
  .rr-dispatch-p2 {
    font-size: 14px;
    line-height: 1.7;
    color: var(--on-surface-variant);
    margin: 0;
  }

  .rr-dispatch-author {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 14px;
    padding-bottom: 4px;
  }
  .rr-dispatch-author-row {
    display: flex;
    align-items: center;
    gap: 10px;
  }
  .rr-dispatch-author-text { text-align: right; }
  .rr-dispatch-author-name {
    font-size: 13px;
    margin: 0;
    color: var(--on-surface);
  }
  .rr-dispatch-author-role {
    font-size: 11px;
    margin: 1px 0 0;
    color: var(--on-surface-variant);
  }
  .rr-dispatch-author-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--ink);
    color: #fffcf7;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: var(--font-serif);
    font-style: italic;
    font-size: 14px;
    flex-shrink: 0;
  }
  .rr-dispatch-cta {
    font-family: var(--font-sans);
    font-size: 11px;
    letter-spacing: 1.2px;
    color: var(--pigment-terracotta);
    text-transform: uppercase;
    text-decoration: none;
    padding-bottom: 2px;
    border-bottom: 1px solid var(--pigment-terracotta);
    white-space: nowrap;
  }
  .rr-dispatch-cta:hover { opacity: 0.78; }

  @media (max-width: 767px) {
    .rr-dispatch { padding: 28px 24px; }
    .rr-dispatch-title { font-size: 24px; }
    .rr-dispatch-body { grid-template-columns: 1fr; gap: 22px; }
    .rr-dispatch-author {
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      order: -1;
    }
  }
</style>
