---
import Layout from "./Layout.astro";
import config from "@/config";

type Props = {
  title?: string;
  description?: string;
  ogImage?: string;
  canonicalURL?: string;
  pubDatetime?: Date;
  modDatetime?: Date | null;
};

const { site } = config;

const { title, description, ogImage, canonicalURL, pubDatetime, modDatetime } =
  Astro.props;

const structuredData = {
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  headline: title ?? site.title,
  image: ogImage,
  ...(pubDatetime && { datePublished: pubDatetime.toISOString() }),
  ...(modDatetime && { dateModified: modDatetime.toISOString() }),
  author: [
    {
      "@type": "Person",
      name: site.author,
      ...(site.profile && { url: site.profile }),
    },
  ],
};
---

<Layout {title} {description} {ogImage} {canonicalURL}>
  <Fragment slot="head">
    <!-- Override og:type for article pages -->
    <meta property="og:type" content="article" />

    {
      pubDatetime && (
        <meta
          property="article:published_time"
          content={pubDatetime.toISOString()}
        />
      )
    }
    {
      modDatetime && (
        <meta
          property="article:modified_time"
          content={modDatetime.toISOString()}
        />
      )
    }

    <script
      type="application/ld+json"
      is:inline
      set:html={JSON.stringify(structuredData)}
    />
  </Fragment>

  <slot />
</Layout>
