import { z } from "zod"; const isoDate = z.string().refine((value) => { if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) return false; const date = new Date(`${value}T00:00:00Z`); return !Number.isNaN(date.valueOf()) && date.toISOString().startsWith(value); }, "Ungültiges ISO-Datum"); const time = z.string().regex(/^([01]\d|2[0-3]):[0-5]\d$/); export const eventSchema = z.object({ id: z.string().min(1), slug: z.string().regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/), title: z.string().min(1), summary: z.string().optional(), startDate: isoDate, startTime: time.optional(), endDate: isoDate.optional(), endTime: time.optional(), timezone: z.literal("Europe/Berlin"), location: z.string().optional(), organizer: z.string().optional(), details: z.array(z.string().min(1)).optional(), flyer: z.object({ src: z.string().startsWith("/images/events/"), alt: z.string().min(1), section: z.enum(["top", "bottom"]) }).optional(), sourceUrl: z.union([z.string().url(), z.string().regex(/^\/(?!\/)[^\s]*$/)]).optional(), sourceName: z.string().optional(), lastVerified: isoDate, status: z.enum(["verified", "dynamic", "needs-verification", "archived"]), privacyApproved: z.boolean().optional(), }).superRefine((event, context) => { if ((event.endDate ?? event.startDate) < event.startDate) context.addIssue({ code: "custom", message: "Das Enddatum liegt vor dem Startdatum." }); if (event.location && /\b\d+[a-z]?\b/i.test(event.location) && event.privacyApproved !== true) context.addIssue({ code: "custom", path: ["privacyApproved"], message: "Eine Adresse benötigt privacyApproved=true." }); if (["verified", "dynamic"].includes(event.status) && (!event.sourceName || !event.sourceUrl)) context.addIssue({ code: "custom", path: ["sourceUrl"], message: "Veröffentlichte Termine benötigen eine Quelle." }); }); export const newsSchema = z.object({ id: z.string().min(1), slug: z.string().regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/), title: z.string().min(1), teaser: z.string().min(1), body: z.string().min(1), publishedAt: isoDate, updatedAt: isoDate.optional(), image: z.object({ src: z.string(), alt: z.string().min(1), copyright: z.string().optional() }).optional(), sourceUrl: z.string().url().optional(), sourceName: z.string().optional(), lastVerified: isoDate, status: z.enum(["published", "draft", "archived"]), }); export const sourceSchema = z.object({ id: z.string().min(1), name: z.string().min(1), url: z.string().url(), type: z.string().min(1), lastVerified: isoDate, }); export type Event = z.infer; export type NewsItem = z.infer; export type Source = z.infer; export function assertUnique(items: T[], key: (item: T) => string, label: string): void { const seen = new Set(); for (const item of items) { const value = key(item); if (seen.has(value)) throw new Error(`Doppelter ${label}: ${value}`); seen.add(value); } }