import { defineCollection, reference } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';

// Shared per-locale layout:
//   src/content/posts/<locale>/…      — posts
//   src/content/categories/<locale>/… — categories
// Entry `id` is always `<locale>/<slug>`. A blog post's `category` references a
// category by that id (e.g. "de/technik" or "en/tech").

// Optional `translationKey`: entries in different locales that represent the
// same logical piece of content share one key. Used to wire up the language
// switcher so it points at the translated URL instead of 404-ing.

const posts = defineCollection({
	loader: glob({ base: './src/content/posts', pattern: '{de,en}/**/*.{md,mdx}' }),
	schema: ({ image }) =>
		z.object({
			title: z.string(),
			description: z.string(),
			pubDate: z.coerce.date(),
			updatedDate: z.coerce.date().optional(),
			heroImage: z.optional(image()),
			heroAlt: z.string().optional(),
			hideHero: z.boolean().optional(),
			category: z.optional(reference('categories')),
			// Free-form tags (aka Stichwörter). Plain strings kept inline on each
			// post; no separate collection. The tag listing pages aggregate them
			// across posts per locale.
			tags: z.array(z.string()).optional(),
			translationKey: z.string().optional(),
			// Series linking: posts that belong to a multi-part series name the
			// parent's slug here. seriesOrder positions the entry within the
			// series; the parent is treated as order 0 if it doesn't set one.
			seriesParent: z.string().optional(),
			seriesOrder: z.number().int().optional(),
			// Project-style frontmatter (demo/repo links shown next to the title).
			url: z.string().url().optional(),
			repo: z.string().url().optional(),
			// Render flags.
			toc: z.boolean().optional(),
			draft: z.boolean().optional(),
		}),
});

const categories = defineCollection({
	loader: glob({ base: './src/content/categories', pattern: '{de,en}/**/*.md' }),
	schema: z.object({
		name: z.string(),
		description: z.string().optional(),
		translationKey: z.string().optional(),
	}),
});

export const collections = { posts, categories };
