import { db } from "@/lib/db"
import { notFound } from "next/navigation"
import { CollaboratorForm } from "@/components/admin/collaborator-form"
import { Button } from "@/components/ui/button"
import { updateCollaborator, deleteCollaborator } from "../actions"

export const metadata = { title: "Admin — Modifier le collaborateur" }

export default async function EditCollaboratorPage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params
  const collaborator = await db.collaborator.findUnique({ where: { id } })

  if (!collaborator) notFound()

  const updateWithId = updateCollaborator.bind(null, id)
  const deleteWithId = deleteCollaborator.bind(null, id)

  return (
    <div>
      <div className="flex items-center justify-between mb-8">
        <h1 className="text-2xl font-heading font-bold uppercase tracking-tight text-[var(--text)]">
          Modifier « {collaborator.name} »
        </h1>
        <form action={deleteWithId}>
          <Button type="submit" variant="destructive" size="sm">
            Supprimer
          </Button>
        </form>
      </div>
      <CollaboratorForm
        action={updateWithId}
        submitLabel="Enregistrer"
        defaultValues={{
          name: collaborator.name,
          role: collaborator.role as "Styliste" | "Artiste" | "Graphiste" | "Agent IA" | "Électronicien",
          isAI: collaborator.isAI,
          photo: collaborator.photo,
          universe: collaborator.universe,
          background: collaborator.background,
          influences: collaborator.influences,
        }}
      />
    </div>
  )
}
