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

const projects = defineCollection({
  loader: glob({ pattern: "**/*.mdx", base: "./projects" }),
  schema: z.object({
    title: z.string(),
    role: z.string(),
    type: z.string(),
    date: z.date(),
    description: z.string(),
    slug: z.string(),
    ongoing: z.boolean().optional().default(false),
    keyFigure: z
      .array(
        z.object({
          title: z.string(),
          name: z.string(),
          href: z.string().optional()
        })
      )
      .optional(),
    frontPage: z
      .object({
        order: z.number()
      })
      .optional(),
    images: z.object({
      hero: z.object({
        src: z.preprocess(
          (val) => `/src/assets/img/project-heros/${val}`,
          z.string()
        ),
        alt: z.string()
      }),
      other: z
        .preprocess((val) => `/src/assets/img/projects/${val}`, z.string())
        .optional()
    }),
    externalLinks: z
      .array(
        z.object({
          name: z.string(),
          href: z.string(),
          icon: z.string()
        })
      )
      .optional()
  })
});

const awards = defineCollection({
  loader: glob({ pattern: "**/*.mdx", base: "./src/assets/awards" }),
  schema: z.object({
    title: z.string(),
    giver: z.string(),
    date: z.date()
  })
});

const tracks = defineCollection({
  loader: glob({ pattern: "**/*.json", base: "./src/assets/tracks" }),
  schema: ({ image }) =>
    z.object({
      src: z.preprocess((val) => `/audio/${val}`, z.string()),
      metadata: z.object({
        title: z.string(),
        subtitle: z.string().optional(),
        extra: z.string().optional(),
        artist: z.string().optional().default("Nathan Cummins"),
        artwork: z
          .preprocess((val) => `/src/assets/img/tracks/${val}`, image())
          .optional()
      }),
      autoQueue: z
        .object({
          order: z.number()
        })
        .optional(),
      card: z
        .object({
          image: z.object({
            src: z.preprocess(
              (val) => `/src/assets/img/tracks/${val}`,
              image()
            ),
            alt: z.string()
          }),
          text: z.object({
            primary: z.string(),
            secondary: z.string()
          })
        })
        .optional()
    })
});

export const collections = { projects, awards, tracks };
