---
/**
 * Alle CMS-Pages: URL-Pfad = slug aus dem CMS (ein oder mehrere Segmente, z. B. about, bi/vachdorf).
 * Keine fest verdrahteten Fallbacks — getStaticPaths kommt nur aus getPages().
 */
import Layout from "../layouts/Layout.astro";
import ContentRows from "../components/ContentRows.svelte";
import { getPages, getPageBySlug } from "../lib/cms";
import { resolveContentImages } from "../lib/rusty-image";
import {
  resolvePostOverviewBlocks,
  resolveSearchableTextBlocks,
  resolveCalendarBlocks,
} from "../lib/blog-utils";
import { PAGE_RESOLVE } from "../lib/constants";
import { T } from "../lib/translations";

export const prerender = true;

export async function getStaticPaths() {
  try {
    const pages = await getPages({ locale: "de" });
    const seen = new Set<string>();
    const paths: { params: { slug: string } }[] = [];
    for (const p of pages) {
      // Inline: Astro extrahiert getStaticPaths — keine lokalen Helfer aus dem Frontmatter.
      const path = (p.slug ?? p._slug ?? "").replace(/^\//, "").trim();
      if (!path) continue;
      if (seen.has(path)) continue;
      seen.add(path);
      paths.push({ params: { slug: path } });
    }
    return paths;
  } catch (e) {
    console.error("getStaticPaths ([...slug]):", e);
    return [];
  }
}

const slugPath = (Astro.params.slug ?? "").trim();
const page = await getPageBySlug(slugPath, {
  locale: "de",
  resolve: PAGE_RESOLVE,
});

if (!page) {
  return Astro.redirect("/404");
}

await resolveContentImages(page);
const tagsMap = await resolvePostOverviewBlocks(page);
await resolveSearchableTextBlocks(page, tagsMap);
await resolveCalendarBlocks(page);
const translations = Astro.locals.translations ?? {};
---

<Layout
  title={page.seoTitle ?? page.headline ?? page.linkName ?? slugPath}
  description={page.seoDescription ?? page.subheadline}
  headline={page.headline ?? page.linkName ?? page.slug ?? page._slug}
  subheadline={page.subheadline}
  topFullwidthBanner={page.topFullwidthBanner}
  showBannerInLayout={!!page.topFullwidthBanner}
  breadcrumbItems={[
    { href: "/", translationKey: T.nav_start },
    { label: page.headline ?? page.linkName ?? page.slug ?? page._slug ?? slugPath },
  ]}
>
  <article>
    <ContentRows client:load layout={page} class="mt-8" translations={translations} />
  </article>
</Layout>
