import type { PremiumTierConfig } from '@nexumi/shared';
import { PremiumManager } from '@/components/owner/premium-manager';
import { getLocale, t } from '@/lib/i18n';
import { ensurePremiumTierConfigs } from '@/lib/premium';
import { prisma } from '@/lib/prisma';

export default async function OwnerPremiumPage() {
  const [locale, tiers, guilds, users] = await Promise.all([
    getLocale(),
    ensurePremiumTierConfigs(prisma),
    prisma.guildPremiumAssignment.findMany({ orderBy: { updatedAt: 'desc' } }),
    prisma.userPremiumAssignment.findMany({ orderBy: { updatedAt: 'desc' } })
  ]);

  return (
    <div className="space-y-6">
      <div>
        <h1 className="text-2xl font-semibold">{t(locale, 'owner.premium.title')}</h1>
        <p className="text-sm text-muted-foreground">{t(locale, 'owner.premium.subtitle')}</p>
      </div>
      <PremiumManager
        initialTiers={tiers as PremiumTierConfig[]}
        initialGuilds={guilds.map((row) => ({
          id: row.id,
          guildId: row.guildId,
          tier: row.tier,
          note: row.note,
          expiresAt: row.expiresAt?.toISOString() ?? null
        }))}
        initialUsers={users.map((row) => ({
          id: row.id,
          userId: row.userId,
          tier: row.tier,
          note: row.note,
          expiresAt: row.expiresAt?.toISOString() ?? null
        }))}
      />
    </div>
  );
}
