import { tasksService } from '@/services/task-management/tasks';
import { tagsService } from '@/services/task-management/tags';
import { AnalyticsService } from '@/services/analytics/analytics';
import { DeadlineAnalyticsService } from '@/services/analytics/deadline-analytics';
import { TagAnalyticsService } from '@/services/analytics/tag-analytics';
import { HomePageClient } from '@/components/HomePageClient';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';

// Force dynamic rendering (no static generation)
export const dynamic = 'force-dynamic';

export default async function HomePage() {
  // Récupérer l'utilisateur connecté
  const session = await getServerSession(authOptions);
  const userId = session?.user?.id;

  // Si pas d'utilisateur connecté, retourner une page vide ou rediriger
  if (!userId) {
    return (
      <div className="min-h-screen flex items-center justify-center">
        <div className="text-center">
          <h1 className="text-2xl font-bold mb-4">Connexion requise</h1>
          <p className="text-gray-600">
            Veuillez vous connecter pour accéder à votre tableau de bord.
          </p>
        </div>
      </div>
    );
  }

  // SSR - Récupération des données côté serveur
  const [
    initialTasks,
    initialTags,
    initialStats,
    productivityMetrics,
    deadlineMetrics,
    tagMetrics,
  ] = await Promise.all([
    tasksService.getTasks(userId),
    tagsService.getTags(userId),
    tasksService.getTaskStats(userId),
    AnalyticsService.getProductivityMetrics(userId),
    DeadlineAnalyticsService.getDeadlineMetrics(userId),
    TagAnalyticsService.getTagDistributionMetrics(userId),
  ]);

  return (
    <HomePageClient
      initialTasks={initialTasks}
      initialTags={initialTags}
      initialStats={initialStats}
      productivityMetrics={productivityMetrics}
      deadlineMetrics={deadlineMetrics}
      tagMetrics={tagMetrics}
    />
  );
}
