import { z } from "zod";
import { CATEGORY_IDS, type CategoryId } from "@/config/blog/categories";
import { isAuthorId } from "@/config/blog/authors";

const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;

export const frontmatterSchema = z.object({
  title: z.string().min(1),
  description: z.string().min(1),
  slug: z
    .string()
    .min(1)
    .regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/, "slug must be kebab-case"),
  date: z.string().regex(ISO_DATE, "date must be YYYY-MM-DD"),
  updated: z.string().regex(ISO_DATE).optional(),
  category: z.enum(CATEGORY_IDS as unknown as [CategoryId, ...CategoryId[]]),
  author: z.string().refine(isAuthorId, "unknown author id"),
  cover: z.string().min(1),
  coverAlt: z.string().min(1),
  draft: z.boolean().optional().default(false),
  featured: z.boolean().optional().default(false),
});

type Frontmatter = z.infer<typeof frontmatterSchema>;

export type PostMeta = Frontmatter & {
  postId: string; // directory name, language-neutral
  locale: "en" | "fr";
  readingMinutes: number;
};

export type Post = PostMeta & { body: string }; // raw MDX body (compiled in the page)
