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(),
        status: 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(),
        active: z.number(),
        disabled: 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(),
        totalItems: z.number().optional(),
    }),
    recentEvents: z.array(RecentEventSchema),
    activeLootdrops: z.array(z.object({
        rewardAmount: z.number(),
        currency: z.string(),
        createdAt: z.string(),
        expiresAt: z.string().nullable(),
    })).optional(),
    lootdropState: z.object({
        monitoredChannels: z.number(),
        hottestChannel: z.object({
            id: z.string(),
            messages: z.number(),
            progress: z.number(),
            cooldown: z.boolean(),
        }).nullable(),
        config: z.object({
            requiredMessages: z.number(),
            dropChance: z.number(),
        }),
    }).optional(),
    leaderboards: z.object({
        topLevels: z.array(z.object({
            username: z.string(),
            level: z.number(),
        })),
        topWealth: z.array(z.object({
            username: z.string(),
            balance: z.string(),
        })),
        topNetWorth: z.array(z.object({
            username: z.string(),
            netWorth: z.string(),
        })),
    }).optional(),
    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(),
        status: z.string().nullable(),
    }),
    guilds: z.number(),
    ping: z.number(),
    cachedUsers: z.number(),
    commandsRegistered: z.number(),
    commandsKnown: 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>;
export const ActivityDataSchema = z.object({
    hour: z.string(),
    commands: z.number(),
    transactions: z.number(),
});

export type ActivityData = z.infer<typeof ActivityDataSchema>;
