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

const news = defineCollection({
    //loader: glob({ pattern: "**/*.md", base: "./src/content/news" }),
    loader: async () => {
        const response = await fetch("http://localhost:3000/api/news?depth=1&draft=false&locale=es&trash=false");
        const rawData = await response.json();

        return rawData.docs.map((item: any) => {
            item.id = item.id.toString()
            return item

        });
    },
    schema: ({ image }) => z.object({
        locale: z.string().describe("News main language"),
        title: z.string(),
        date: z.string(),
        slug: z.string(),
        draft: z.boolean().optional().nullable(),
        place: z.string().optional().nullable(),
        order: z.number().optional().nullable(),
        city: z.string().optional().nullable(),
        state: z.string().optional().nullable(),
        country: z.string().optional().nullable(),
        thumbnail: z.object({
            image: image().optional().nullable(),
            url: z.string().optional().nullable()
        }).optional().nullable().describe("Main news thumbnail image"),
        //thumbnail_square: image().optional().describe("Main news thumbnail square image"),
        youtube: z.string().optional(),
        tags: z.array(z.string()).optional().describe("News tags"),
        gallery: z.array(z.object({
            image: z.object({
                url: z.string().optional().nullable()
            }).optional().nullable()
        })).optional().nullable(),
        body: z.string().nullable().optional()
    }),
});

const editorial = defineCollection({
    loader: glob({ pattern: "**/*.md", base: "./src/content/editorial" }),
    schema: ({ image }) => z.object({
        locale: z.string().describe("Editorial main language"),
        title: z.string(),
        date: z.date(),
        slug: z.string(),
        draft: z.boolean().optional(),
        place: z.string().optional(),
        order: z.number().optional(),
        city: z.string().optional(),
        state: z.string().optional(),
        country: z.string().optional(),
        thumbnail: image().optional().describe("Main editorial thumbnail image"),
        thumbnail_square: image().optional().describe("Main editorial thumbnail square image"),
        youtube: z.string().optional(),
        tags: z.array(z.string()).optional().describe("Editorial tags"),
        gallery: z.array(z.object({
            image: image().optional(),
            text: z.string().optional(),
            text_alt: z.string().optional(),
        })).optional().nullable()
    }),
});

export const collections = { news, editorial };
