import { GuildBackupManager } from '@/components/modules/guild-backup-manager';
import { requireAuthOrRedirect } from '@/lib/auth';
import { fetchDiscordUserGuildsCached } from '@/lib/discord-oauth';
import { getLocale, t } from '@/lib/i18n';
import { listGuildBackupsDashboard } from '@/lib/module-configs/guildbackup';

interface BackupPageProps {
  params: Promise<{ guildId: string }>;
}

export default async function BackupPage({ params }: BackupPageProps) {
  const { guildId } = await params;
  const session = await requireAuthOrRedirect();
  const [locale, backups, discordGuilds] = await Promise.all([
    getLocale(),
    listGuildBackupsDashboard(guildId),
    fetchDiscordUserGuildsCached(session.accessToken)
  ]);
  const isOwner = discordGuilds.find((guild) => guild.id === guildId)?.owner ?? false;

  return (
    <div className="space-y-6">
      <div>
        <h1 className="text-2xl font-semibold">{t(locale, 'modules.guildbackup.label')}</h1>
        <p className="text-sm text-muted-foreground">{t(locale, 'modules.guildbackup.description')}</p>
      </div>
      <GuildBackupManager guildId={guildId} initialBackups={backups} isOwner={isOwner} />
    </div>
  );
}
