import FeedbackManagement from "@/components/admin/FeedbackManagement";
import { Card } from "@/components/ui";
import { eventFeedbackService } from "@/services/events/event-feedback.service";

export const dynamic = "force-dynamic";

export default async function AdminFeedbacksPage() {
  const [feedbacksRaw, statistics] = await Promise.all([
    eventFeedbackService.getAllFeedbacks(),
    eventFeedbackService.getFeedbackStatistics(),
  ]);

  // Type assertion car getAllFeedbacks inclut event et user par défaut
  const feedbacks = feedbacksRaw as unknown as Array<{
    id: string;
    rating: number;
    comment: string | null;
    isRead: boolean;
    createdAt: Date;
    event: {
      id: string;
      name: string;
      date: Date;
      type: string;
    };
    user: {
      id: string;
      username: string;
      email: string;
      avatar: string | null;
      score: number;
    };
  }>;

  // Sérialiser les dates pour le client
  const serializedFeedbacks = feedbacks.map((feedback) => ({
    id: feedback.id,
    rating: feedback.rating,
    comment: feedback.comment,
    isRead: feedback.isRead,
    createdAt: feedback.createdAt.toISOString(),
    event: {
      id: feedback.event.id,
      name: feedback.event.name,
      date: feedback.event.date.toISOString(),
      type: feedback.event.type,
    },
    user: {
      id: feedback.user.id,
      username: feedback.user.username,
      email: feedback.user.email,
      avatar: feedback.user.avatar,
      score: feedback.user.score,
    },
  }));

  const serializedStatistics = statistics.map((stat) => ({
    eventId: stat.eventId,
    eventName: stat.eventName,
    eventDate: stat.eventDate?.toISOString() ?? null,
    eventType: stat.eventType,
    averageRating: stat.averageRating,
    feedbackCount: stat.feedbackCount,
  }));

  return (
    <Card variant="dark" className="p-6">
      <h2 className="text-2xl font-gaming font-bold mb-6 text-pixel-gold">
        Gestion des Feedbacks
      </h2>
      <FeedbackManagement
        initialFeedbacks={serializedFeedbacks}
        initialStatistics={serializedStatistics}
      />
    </Card>
  );
}
