import { Metadata } from 'next';
import { NotesPageClient } from './NotesPageClient';
import { notesService } from '@/services/notes';
import { foldersService } from '@/services/folders';
import { tagsService } from '@/services/task-management/tags';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';

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

export const metadata: Metadata = {
  title: 'Notes - Tower Control',
  description: 'Gestionnaire de notes markdown',
};

export default async function NotesPage() {
  const session = await getServerSession(authOptions);

  if (!session?.user?.id) {
    return (
      <div className="min-h-screen bg-[var(--background)] flex items-center justify-center">
        <div className="text-center">
          <h1 className="text-2xl font-bold text-[var(--foreground)] mb-4">
            Non autorisé
          </h1>
          <p className="text-[var(--muted-foreground)]">
            Vous devez être connecté pour accéder aux notes.
          </p>
        </div>
      </div>
    );
  }

  // SSR - Récupération des données côté serveur
  const initialNotes = await notesService.getNotes(session.user.id);
  const initialTags = await tagsService.getTags(session.user.id);
  const initialFolders = await foldersService.getFolders(session.user.id);

  return (
    <NotesPageClient
      initialNotes={initialNotes}
      initialTags={initialTags}
      initialFolders={initialFolders}
    />
  );
}
