import { userPreferencesService } from '@/services/core/user-preferences';
import { getJiraAnalytics } from '@/actions/jira-analytics';
import { JiraDashboardPageClient } from './JiraDashboardPageClient';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';

// Force dynamic rendering
export const dynamic = 'force-dynamic';

export default async function JiraDashboardPage() {
  const session = await getServerSession(authOptions);
  if (!session?.user?.id) {
    return <div>Non authentifié</div>;
  }

  // Récupérer la config Jira côté serveur
  const jiraConfig = await userPreferencesService.getJiraConfig(
    session.user.id
  );

  // Récupérer les analytics côté serveur (utilise le cache du service)
  let initialAnalytics = null;
  if (jiraConfig.enabled && jiraConfig.projectKey) {
    const analyticsResult = await getJiraAnalytics(false); // Utilise le cache
    if (analyticsResult.success) {
      initialAnalytics = analyticsResult.data;
    }
  }

  return (
    <JiraDashboardPageClient
      initialJiraConfig={jiraConfig}
      initialAnalytics={initialAnalytics}
    />
  );
}
