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

const stripIndex = ({ entry }: { entry: string }) =>
  entry.replace(/\/index\.mdx$/, '');

const reviews = defineCollection({
  loader: glob({
    pattern: '**/index.mdx',
    base: './src/content/reviews',
    generateId: stripIndex,
  }),
  schema: z.object({
    name: z.string(),
    subtitle: z.string(),
    date: z.coerce.date(),
    category: z.string(),
    tags: z.array(z.string()).default([]),
    link: z.string().url().optional(),
    linkText: z.string().optional(),
    description: z.string().optional(),
    source: z.string().optional(),
    fromFind: z.string().optional(),
  }),
});

const posts = defineCollection({
  loader: glob({
    pattern: '**/index.mdx',
    base: './src/content/posts',
    generateId: stripIndex,
  }),
  schema: z.object({
    title: z.string(),
    date: z.coerce.date(),
    category: z.string(),
    tags: z.array(z.string()).default([]),
    description: z.string().optional(),
  }),
});

const find = defineCollection({
  loader: glob({
    pattern: '**/index.mdx',
    base: './src/content/find',
    generateId: stripIndex,
  }),
  schema: z.object({
    name: z.string(),
    subtitle: z.string().optional(),
    date: z.coerce.date(),
    link: z.string().url(),
    linkText: z.string().optional(),
    source: z.string(),
    topics: z.array(z.string()).default([]),
    tags: z.array(z.string()).default([]),
    description: z.string().optional(),
    promotedTo: z.string().optional(),
  }),
});

const finds = defineCollection({
  loader: glob({
    pattern: '**/index.mdx',
    base: './src/content/finds',
    generateId: stripIndex,
  }),
  schema: z.object({
    title: z.string(),
    date: z.coerce.date(),
    category: z.string().default('finds'),
    tags: z.array(z.string()).default([]),
    description: z.string().optional(),
    blurb: z.string().optional(),
    items: z.array(z.string()).min(1),
  }),
});

export const collections = { reviews, posts, find, finds };
