import { z } from "zod";

export const RecentEventSchema = z.object({
    type: z.enum(['success', 'error', 'info', 'warn']),
    message: z.string(),
    timestamp: z.union([z.date(), z.string().datetime()]),
    icon: z.string().optional(),
});

export type RecentEvent = z.infer<typeof RecentEventSchema>;

export const DashboardStatsSchema = z.object({
    bot: z.object({
        name: z.string(),
        avatarUrl: z.string().nullable(),
    }),
    guilds: z.object({
        count: z.number(),
        changeFromLastMonth: z.number().optional(),
    }),
    users: z.object({
        active: z.number(),
        total: z.number(),
        changePercentFromLastMonth: z.number().optional(),
    }),
    commands: z.object({
        total: z.number(),
        changePercentFromLastMonth: z.number().optional(),
    }),
    ping: z.object({
        avg: z.number(),
        changeFromLastHour: z.number().optional(),
    }),
    economy: z.object({
        totalWealth: z.string(),
        avgLevel: z.number(),
        topStreak: z.number(),
    }),
    recentEvents: z.array(RecentEventSchema),
    uptime: z.number(),
    lastCommandTimestamp: z.number().nullable(),
    maintenanceMode: z.boolean(),
});

export type DashboardStats = z.infer<typeof DashboardStatsSchema>;

export const ClientStatsSchema = z.object({
    bot: z.object({
        name: z.string(),
        avatarUrl: z.string().nullable(),
    }),
    guilds: z.number(),
    ping: z.number(),
    cachedUsers: z.number(),
    commandsRegistered: z.number(),
    uptime: z.number(),
    lastCommandTimestamp: z.number().nullable(),
});

export type ClientStats = z.infer<typeof ClientStatsSchema>;

// Action Schemas
export const MaintenanceModeSchema = z.object({
    enabled: z.boolean(),
    reason: z.string().optional(),
});

// WebSocket Message Schemas
export const WsMessageSchema = z.discriminatedUnion("type", [
    z.object({ type: z.literal("PING") }),
    z.object({ type: z.literal("PONG") }),
    z.object({ type: z.literal("STATS_UPDATE"), data: DashboardStatsSchema }),
    z.object({ type: z.literal("NEW_EVENT"), data: RecentEventSchema }),
]);

export type WsMessage = z.infer<typeof WsMessageSchema>;
