import Link from "next/link";
import { redirect } from "next/navigation";
import { getEvaluation, getTemplates, getUsers } from "@/lib/server-data";
import { EvaluationEditor } from "@/components/EvaluationEditor";

interface PageProps {
  params: Promise<{ id: string }>;
}

export default async function EvaluationDetailPage({ params }: PageProps) {
  const { id } = await params;
  const [evaluation, templates, users] = await Promise.all([
    getEvaluation(id),
    getTemplates(),
    getUsers(),
  ]);

  if (!evaluation) {
    return (
      <div className="py-12 text-center font-mono text-xs text-zinc-600 dark:text-zinc-500">
        Évaluation introuvable.{" "}
        <Link href="/dashboard" className="text-cyan-600 dark:text-cyan-400 hover:underline">
          ← dashboard
        </Link>
      </div>
    );
  }

  if (!users) redirect("/auth/login");

  const templatesForEditor = templates.map((t) => ({
    id: t.id,
    name: t.name,
    dimensions: t.dimensions.map((d) => ({
      id: d.id,
      suggestedQuestions: d.suggestedQuestions,
    })),
  }));

  return (
    <EvaluationEditor
      id={id}
      initialEvaluation={evaluation as unknown as Parameters<typeof EvaluationEditor>[0]["initialEvaluation"]}
      templates={templatesForEditor}
      users={users}
    />
  );
}
