import { notFound } from 'next/navigation';
import { WelcomeForm } from '@/components/modules/welcome-form';
import { requireGuildAccessOrRedirect } from '@/lib/auth';
import { resolveDashboardGuild } from '@/lib/guilds';
import { getLocale, t } from '@/lib/i18n';
import { getWelcomeDashboard } from '@/lib/module-configs/welcome';
import { buildWelcomePreviewVars } from '@/lib/welcome-preview-vars';

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

export default async function WelcomePage({ params }: WelcomePageProps) {
  const { guildId } = await params;
  const session = await requireGuildAccessOrRedirect(guildId);
  const [locale, config, resolved] = await Promise.all([
    getLocale(),
    getWelcomeDashboard(guildId),
    resolveDashboardGuild(session, guildId)
  ]);

  if (!resolved) {
    notFound();
  }

  return (
    <div className="space-y-6">
      <div>
        <h1 className="text-2xl font-semibold">{t(locale, 'modules.welcome.label')}</h1>
        <p className="text-sm text-muted-foreground">{t(locale, 'modules.welcome.description')}</p>
      </div>
      <WelcomeForm
        guildId={guildId}
        initialValue={config}
        previewVars={buildWelcomePreviewVars(session.user, resolved.guild)}
      />
    </div>
  );
}
