---
import { getCollection, render } from "astro:content";
import { Image } from "astro:assets";
import Base from "../../layouts/Base.astro";
import Cover from "../../components/Cover.astro";
import RobotScore from "../../components/RobotScore.astro";
import { categorySlug, categoryHue } from "../../lib/category";
import { emojify } from "../../lib/emojify";
import { SITE_AUTHOR_NAME, SITE_AUTHOR_EMAIL } from "../../lib/site";
import { isPublished } from "../../lib/publish";

export async function getStaticPaths() {
  const all = await getCollection("reviews");
  return all.map((entry) => ({
    params: { slug: entry.id },
    props: { entry },
  }));
}

const { entry } = Astro.props;
const { Content } = await render(entry);
const d = entry.data;

// Screenshots liggen co-located naast de content (src/assets/reviews/<slug>/screenshot.png),
// niet elke review heeft er per se een (bv. heel oude of handmatig geschreven reviews),
// dus we checken op aanwezigheid en vallen anders terug op de gegenereerde cover.
const screenshots = import.meta.glob<{ default: ImageMetadata }>(
  "/src/assets/reviews/*/screenshot.png",
  { eager: true },
);
const screenshot = screenshots[`/src/assets/reviews/${entry.id}/screenshot.png`];

const dateFormatter = new Intl.DateTimeFormat("nl-NL", {
  day: "numeric",
  month: "long",
  year: "numeric",
});
---

<Base title={emojify(d.title)} description={emojify(d.verdict)} noindex={!isPublished(d)}>
  <article class="review">
    <p class="breadcrumb"><a href="/">← Alle reviews</a></p>

    <h1>{emojify(d.title)}</h1>
    <div class="review-meta">
      {d.date && <span class="tabnum">{dateFormatter.format(d.date)}</span>}
      {d.date && <span>·</span>}
      <a href={`/categorieen#${categorySlug(d.category)}`}>{emojify(d.category)}</a>
      <span>·</span>
      <span>{emojify(d.location)}</span>
      <span>·</span>
      <a class="company-link" href={d.website} target="_blank" rel="noopener">{emojify(d.company)} →</a>
    </div>

    <div class="verdict">
      <div>
        <div class="label">Eindoordeel</div>
        <p class="oneliner">{emojify(d.verdict)}</p>
      </div>
      <div class="score-block">
        <RobotScore score={d.score} size="lg" showNumber={false} />
        <span class="score-num tabnum">{d.score} / 5</span>
      </div>
    </div>

    <dl class="specs">
      <div>
        <dt>Voor wie</dt>
        <dd>{emojify(d.for_who)}</dd>
      </div>
      <div>
        <dt>Vanaf</dt>
        <dd class="tabnum">{emojify(d.price_from)}</dd>
      </div>
      <div>
        <dt>Hosting</dt>
        <dd>{emojify(d.hosting)}</dd>
      </div>
    </dl>

    {
      screenshot ? (
        <a class="hero-shot" href={d.website} target="_blank" rel="noopener">
          <Image src={screenshot.default} alt={`Screenshot van ${d.company}`} widths={[400, 800, 1200]} sizes="(min-width: 820px) 772px, 100vw" />
        </a>
      ) : (
        <Cover hue={categoryHue(d.category)} logo={d.logo} company={d.company} size="hero" />
      )
    }

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

<style>
  .breadcrumb {
    font-family: var(--mono);
    font-size: 13px;
    color: var(--muted);
    margin: 30px 0 20px;
  }
  .breadcrumb a { color: var(--muted); }

  .hero-shot {
    display: block;
    margin-bottom: 26px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    overflow: hidden;
    transition: border-color 0.15s ease;
  }
  .hero-shot:hover { border-color: var(--accent); }
  .hero-shot :global(img) { display: block; width: 100%; height: auto; }

  article.review h1 {
    font-size: 40px;
    margin: 0 0 10px;
    text-wrap: balance;
    letter-spacing: -0.01em;
  }
  .review-meta {
    font-family: var(--mono);
    font-size: 13.5px;
    color: var(--muted);
    margin-bottom: 22px;
    display: flex;
    gap: 14px;
    flex-wrap: wrap;
  }
  .review-meta a { color: var(--muted); text-decoration-color: var(--line); }
  .review-meta a:hover { color: var(--accent); text-decoration-color: var(--accent); }
  .review-meta a.company-link { color: var(--accent); text-decoration-color: currentColor; }

  .verdict {
    background: var(--paper-raised);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 22px 24px;
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 12px 24px;
    align-items: center;
    margin-bottom: 36px;
  }
  .verdict .label {
    font-family: var(--mono);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted);
    margin-bottom: 6px;
  }
  .verdict .oneliner { margin: 0; font-size: 17px; max-width: 46ch; }
  .verdict .score-block { text-align: right; }
  .verdict .score-block .score-num {
    font-family: var(--mono);
    font-size: 16px;
    color: var(--muted);
    display: block;
    margin: 6px 0 0;
  }

  .specs {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 18px;
    margin: 0 0 36px;
    padding: 18px 0;
    border-top: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
  }
  .specs dt {
    font-family: var(--mono);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    margin: 0 0 4px;
  }
  .specs dd { margin: 0; font-size: 16px; }

  /* geen max-width op .body zelf: tekstelementen krijgen 'm individueel, zodat
     een afbeelding in de lopende tekst wél de volle artikelbreedte kan gebruiken.
     p:has(img) sluit specifiek de <p> uit die de markdown-renderer om een losse
     afbeelding heen zet (![]() wordt altijd <p><img></p>), anders zou die p z'n
     eigen 64ch nog steeds doorgeven aan het kind-element. */
  .body :global(h2) { max-width: 64ch; font-size: 22px; margin: 42px 0 12px; letter-spacing: -0.005em; }
  .body :global(p) { max-width: 64ch; margin: 0 0 17px; }
  .body :global(p:has(img)) { max-width: none; margin: 0 0 20px; }
  .body :global(img) {
    max-width: 100%;
    height: auto;
    display: block;
    border: 1px solid var(--line);
    border-radius: var(--radius);
  }
  .body :global(blockquote) {
    max-width: 64ch;
    margin: 0 0 20px;
    padding: 2px 20px;
    border-left: 3px solid var(--accent);
    background: var(--paper-raised);
    border-radius: 0 var(--radius) var(--radius) 0;
    color: var(--muted);
    font-style: italic;
  }
  .body :global(blockquote p) { margin: 14px 0; }

  /* Markdown-tabellen: zelfde breedte als lopende tekst (64ch) */
  .body :global(.table-wrap) {
    max-width: 64ch;
    margin: 0 0 24px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    background: var(--paper);
  }
  .body :global(.table-wrap table) {
    width: 100%;
    margin: 0;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 15px;
    line-height: 1.45;
    border: none;
    background: transparent;
  }
  .body :global(caption) {
    caption-side: bottom;
    padding: 10px 14px 0;
    font-family: var(--mono);
    font-size: 12.5px;
    color: var(--muted);
    text-align: left;
  }
  .body :global(thead) {
    background: var(--paper-raised);
  }
  .body :global(th),
  .body :global(td) {
    padding: 11px 14px;
    text-align: left;
    vertical-align: top;
    border-bottom: 1px solid var(--line);
  }
  .body :global(th) {
    font-family: var(--mono);
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    white-space: nowrap;
  }
  .body :global(th:not(:last-child)),
  .body :global(td:not(:last-child)) {
    border-right: 1px solid var(--line);
  }
  .body :global(tbody tr:last-child th),
  .body :global(tbody tr:last-child td) {
    border-bottom: none;
  }
  .body :global(tbody tr:nth-child(even)) {
    background: color-mix(in srgb, var(--paper-raised) 55%, transparent);
  }
  .body :global(tbody tr:hover) {
    background: var(--accent-soft);
  }
  /* Eerste kolom vaak een label (tool / plan / model) */
  .body :global(tbody th),
  .body :global(tbody td:first-child) {
    font-weight: 600;
    color: var(--ink);
  }
  .body :global(td .tabnum),
  .body :global(th .tabnum) {
    font-variant-numeric: tabular-nums;
  }
  .body :global(td code),
  .body :global(th code) {
    font-family: var(--mono);
    font-size: 0.9em;
    font-weight: 500;
    background: var(--paper-raised);
    border: 1px solid var(--line);
    border-radius: 3px;
    padding: 0.1em 0.35em;
  }

  @media (max-width: 620px) {
    .specs { grid-template-columns: 1fr 1fr; }
    .verdict { grid-template-columns: 1fr; }
    .verdict .score-block { text-align: left; }
    .body :global(th),
    .body :global(td) {
      padding: 9px 11px;
      font-size: 14px;
    }
    .body :global(th) {
      white-space: normal;
    }
  }
</style>
