'use strict';

const z = require('zod');

const dateFields = {
  publishedAt: z.z.coerce.date().optional(),
  createdAt: z.z.coerce.date().optional(),
  updatedAt: z.z.coerce.date().optional()
};
const coreFields = {
  title: z.z.string().min(1, "Title is required"),
  content: z.z.string().min(1, "Content is required"),
  excerpt: z.z.string().min(1, "Excerpt is required"),
  image: z.z.string().optional(),
  published: z.z.boolean().optional().default(false),
  slug: z.z.string().min(1, "Slug is required"),
  tags: z.z.array(
    z.z.union([
      z.z.object({ name: z.z.string() }),
      z.z.object({ id: z.z.string(), name: z.z.string(), slug: z.z.string() })
    ])
  ).optional().default([])
};
const PostDomainSchema = z.z.object({
  id: z.z.string().optional(),
  ...coreFields,
  ...dateFields
});
const createPostSchema = PostDomainSchema.extend({
  slug: PostDomainSchema.shape.slug.optional()
}).omit({ id: true });
const updatePostSchema = PostDomainSchema.extend({
  id: z.z.string()
  // required on update
});

exports.PostDomainSchema = PostDomainSchema;
exports.createPostSchema = createPostSchema;
exports.updatePostSchema = updatePostSchema;
