/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2023 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import { defineCollection, reference, z } from "astro:content";
import { docsLoader } from "@astrojs/starlight/loaders";
import { docsSchema } from "@astrojs/starlight/schema";
import { EventViewConfigSchema } from "@components/event/types";

export const pagesSchema = z.object({
    title: z.string().min(1).max(80),
    description: z.string().min(1).max(120),
    image: z.string().optional(),
    slugs: z.record(z.string(), z.string()).optional(),
});

export const pages = defineCollection({
    type: "content",
    schema: pagesSchema,
});

export const help = defineCollection({
    type: "content",
    schema: z.object({
        title: z.string().min(1).max(80),
        description: z.string().min(1).max(120),
        tags: z.array(z.string()),
        related: z.array(reference("help")).optional(),
    }),
});

export const modes = defineCollection({
    type: "data",
    schema: z.object({
        translationKey: z.string(),
        main: z.boolean(),
        ranked: z.boolean().optional().default(false),
    }),
});

export const downloads = defineCollection({
    type: "data",
    schema: z.object({
        name: z.string(),
        description: z.string(),
        url: z.string().url().or(z.record(z.string(), z.string())),
        sourceUrl: z.string().url().optional(),
    }),
});

export const rules = defineCollection({
    type: "content",
    schema: z.object({
        translationKey: z.string(),
        mode: reference("modes").optional(),
    }),
});

export const announcements = defineCollection({
    type: "content",
    schema: ({ image }) =>
        z.object({
            title: z.string(),
            description: z.string(),
            author: z.string().optional(),
            image: image().optional(),
            tags: z.array(z.string()),
            created: z.date(),
            key: z.string(),
        }),
});

export const publics = defineCollection({
    type: "data",
    schema: ({ image }) =>
        z.object({
            name: z.string(),
            description: z.string(),
            id: z.number().positive(),
            creator: z.string().array().optional(),
            showcase: z.string().url().optional(),
            camera: z
                .object({
                    fov: z.number().optional(),
                    near: z.number().optional(),
                    far: z.number().optional(),
                    distance: z.number().optional(),
                })
                .optional(),
            image: image(),
            alt: image().optional(),
            xray: image().optional(),
            gamemode: reference("modes"),
            "3d": z.boolean().optional().default(true),
        }),
});

export const events = defineCollection({
    type: "content",
    schema: ({ image }) =>
        z.object({
            eventId: z.number().positive(),
            image: image().optional(),
            mode: reference("modes").optional(),
            hideTeamSize: z.boolean().optional().default(false),
            verwantwortlich: z.string().optional(),
            viewConfig: EventViewConfigSchema.optional(),
        }),
});

export const collections = {
    pages: pages,
    help: help,
    modes: modes,
    rules: rules,
    downloads: downloads,
    announcements: announcements,
    publics: publics,
    docs: defineCollection({ loader: docsLoader(), schema: docsSchema() }),
    events: events,
};
