import { prisma } from './prisma';

export async function getPublicStats() {
  const [guildCount, memberLevels, memberEconomies, users] = await Promise.all([
    prisma.guild.count(),
    prisma.memberLevel.findMany({ select: { userId: true }, distinct: ['userId'] }),
    prisma.memberEconomy.findMany({ select: { userId: true }, distinct: ['userId'] }),
    prisma.user.count()
  ]);

  const unique = new Set<string>();
  for (const row of memberLevels) {
    unique.add(row.userId);
  }
  for (const row of memberEconomies) {
    unique.add(row.userId);
  }

  return {
    guildCount,
    approximateUserCount: Math.max(unique.size, users),
    updatedAt: new Date().toISOString()
  };
}
