---
import { getCollection, render } from 'astro:content';
import BaseLayout from '../../layouts/BaseLayout.astro';
import FindsGrid from '../../components/FindsGrid.astro';
import { resolveFinds } from '../../lib/find-items';
import {
  buildOgImage,
  buildBreadcrumbs,
  buildItemList,
  PERSON_AUTHOR,
  absoluteUrl,
} from '../../lib/seo';

export async function getStaticPaths() {
  const finds = await getCollection('finds');
  return finds.map((post) => ({
    params: { slug: post.id },
    props: { post },
  }));
}

const { post } = Astro.props;
const { Content } = await render(post);
const items = await resolveFinds(post.data.items);

const fmt = new Intl.DateTimeFormat('en-US', {
  year: 'numeric',
  month: 'long',
  day: 'numeric',
});

const description =
  post.data.description ?? post.data.blurb ?? `A collection: ${post.data.title}.`;

const ogImage = buildOgImage(
  items[0]?.data.promotedTo,
  'finds',
  `${post.data.title} cover image`
);

const articleSchema: Record<string, unknown> = {
  '@context': 'https://schema.org',
  '@type': 'Article',
  '@id': absoluteUrl(`/finds/${post.id}/#article`),
  headline: post.data.title,
  description,
  url: absoluteUrl(`/finds/${post.id}/`),
  datePublished: post.data.date.toISOString(),
  author: PERSON_AUTHOR,
  image: ogImage.url,
  inLanguage: 'en-US',
};

const itemList = buildItemList(
  items.map((it) => ({ url: `/find/${it.id}/`, name: it.data.name })),
  post.data.title
);

const breadcrumbs = buildBreadcrumbs([
  { name: 'Home', url: '/' },
  { name: post.data.title, url: `/finds/${post.id}/` },
]);
---

<BaseLayout
  title={post.data.title}
  description={description}
  ogType="article"
  ogImage={ogImage}
  publishedTime={post.data.date}
  articleTags={post.data.tags}
  jsonLd={[articleSchema, itemList, breadcrumbs]}
>
  <article>
    <header class="finds-header">
      <h1>{post.data.title}</h1>
      <p class="meta">
        <time datetime={post.data.date.toISOString()}>
          {fmt.format(post.data.date)}
        </time>
      </p>
    </header>

    <div class="body">
      <Content />
    </div>

    <FindsGrid items={items} />

    {
      post.data.tags.length > 0 && (
        <footer class="tags">
          {post.data.tags.map((tag) => (
            <a href={`/tags/${tag}/`} class="tag">#{tag}</a>
          ))}
        </footer>
      )
    }

    <p class="back"><a href="/">← back</a></p>
  </article>

  <style>
    .finds-header {
      margin-bottom: 1.5rem;
    }
    h1 { margin: 0 0 0.35rem; font-size: 1.85rem; }
    .meta {
      margin: 0;
      color: var(--muted);
      font-size: 0.9rem;
    }
    .body {
      margin-bottom: 1.75rem;
      max-width: 42rem;
    }
    .body :global(p) { margin: 0 0 1rem; }
    .body :global(blockquote) {
      margin: 1.25rem 0;
      padding: 0.15rem 0 0.15rem 1rem;
      border-left: 3px solid var(--accent);
      color: var(--fg);
      font-style: normal;
    }
    .body :global(blockquote p) { margin: 0; }
    .tags {
      margin-top: 2.5rem;
      padding-top: 1rem;
      border-top: 1px solid var(--rule);
      display: flex;
      gap: 0.75rem;
      flex-wrap: wrap;
    }
    .tag { font-size: 0.85rem; color: var(--muted); }
    .tag:hover { color: var(--accent); }
    .back { margin-top: 2rem; font-size: 0.9rem; }
  </style>
</BaseLayout>
