import { GuildsManager } from '@/components/owner/owner-forms';
import { getLocale, t } from '@/lib/i18n';
import { enrichOwnerGuildListItems } from '@/lib/owner-guilds';
import { prisma } from '@/lib/prisma';

export default async function OwnerGuildsPage() {
  const locale = await getLocale();
  const [guilds, blacklist] = await Promise.all([
    prisma.guild.findMany({
      include: { settings: true },
      orderBy: { createdAt: 'desc' },
      take: 200
    }),
    prisma.guildBlacklist.findMany()
  ]);
  const blacklisted = new Set(blacklist.map((entry) => entry.guildId));
  const initialGuilds = await enrichOwnerGuildListItems(
    guilds.map((guild) => ({
      id: guild.id,
      locale: guild.settings?.locale ?? 'de',
      createdAt: guild.createdAt.toISOString(),
      blacklisted: blacklisted.has(guild.id)
    }))
  );

  return (
    <div className="space-y-6">
      <div>
        <h1 className="text-2xl font-semibold">{t(locale, 'owner.guilds.title')}</h1>
        <p className="text-sm text-muted-foreground">{t(locale, 'owner.guilds.subtitle')}</p>
      </div>
      <GuildsManager initialGuilds={initialGuilds} />
    </div>
  );
}
