---
import { type CollectionEntry, getCollection, render } from "astro:content";
import dayjs from "dayjs";
import blogPostSchema from "../../utils/schemas/blogPostSchema";
import breadcrumbSchema from "../../utils/schemas/breadcrumbSchema";
import Comments from "../../components/Comments.astro";
import Layout from "../../layouts/BaseLayout.astro";
import personSchema from "../../utils/schemas/personSchema";
import websiteSchema from "../../utils/schemas/websiteSchema";
import { config } from "../../config";

type Props = CollectionEntry<"blog">;

export async function getStaticPaths() {
	const posts = await getCollection("blog", ({ data }) => {
		return data.draft !== true;
	});

	return posts.map((post) => ({
		params: { slug: post.id },
		props: post,
	}));
}

const post = Astro.props;

const { Content, remarkPluginFrontmatter } = await render(post);

const description = post.data.description;
const isBasedOn = post.data.basedOn;
const lang = post.data.lang;
const preview = `/images/preview/${post.id}.png`;
const slug = post.id;
const headline = post.data.title;
const title = `${post.data.title} | Valentin Popov`;

const dateModified = (post.data.dateModified ?? post.data.datePublished).toISOString();
const datePublished = post.data.datePublished.toISOString();
const formattedDate = dayjs(post.data.datePublished.toString()).format("MMMM DD, YYYY");

const siteUrl = new URL("/", Astro.site).toString();

const schema = [
	websiteSchema({ siteUrl, name: config.og.website, description, lang }),
	personSchema({ siteUrl }),
	blogPostSchema({
		siteUrl,
		dateModified,
		datePublished,
		description,
		isBasedOn,
		lang,
		preview,
		slug,
		title: headline,
	}),
	breadcrumbSchema({
		siteUrl,
		items: [
			{ name: "Home", url: "/" },
			{ name: "Blog", url: "/blog/" },
			{ name: headline, url: `/blog/${slug}` },
		],
	}),
];
---

<style lang="scss">
	@use "../../scss/variables" as *;

	p {
		opacity: 0.5;
	}
</style>

<Layout title={title} description={description} preview={preview} lang={lang} schema={schema} ogType="article" publishedTime={datePublished} modifiedTime={dateModified}>
	<article>
		<header>
			<h1>{headline}</h1>

			<p>
				<small>
					Posted
					<time datetime={datePublished} lang="en">{formattedDate}</time>
					<span>&nbsp;•&nbsp;</span>
					<span>{remarkPluginFrontmatter.minutesRead}</span>
				</small>
			</p>
		</header>

		<section>
			<Content />
		</section>

		<section>
			<Comments />
		</section>
	</article>
</Layout>
