import Link from 'next/link';
import { prisma } from '@/lib/prisma';
import { AdminDeleteButton } from '@/components/admin/delete-button';
import type { Locale } from '@/i18n/config';

export default async function AdminProjectsPage({ params: { locale } }: { params: { locale: Locale } }) {
  const projects = await prisma.project.findMany({
    include: { translations: { where: { locale: 'fr' } } }
  });

  return (
    <div>
      <div className="flex items-center justify-between">
        <h1 className="font-display text-2xl font-semibold">Réalisations</h1>
        <Link
          href={`/${locale}/admin/projects/new`}
          className="rounded-full bg-copper-500 px-4 py-2 text-sm font-semibold text-graphite-950"
        >
          + Nouvelle réalisation
        </Link>
      </div>

      <div className="mt-6 overflow-hidden rounded-2xl bg-white">
        <table className="w-full text-sm">
          <thead className="border-b border-graphite-800/10 text-graphite-900/50">
            <tr>
              <th className="px-4 py-3 text-start">Titre</th>
              <th className="px-4 py-3 text-start">Publié</th>
              <th className="px-4 py-3" />
            </tr>
          </thead>
          <tbody>
            {projects.map((p) => (
              <tr key={p.id} className="border-b border-graphite-800/5 last:border-0">
                <td className="px-4 py-3">{p.translations[0]?.title ?? p.slug}</td>
                <td className="px-4 py-3">{p.published ? 'Oui' : 'Non'}</td>
                <td className="px-4 py-3 text-end">
                  <Link href={`/${locale}/admin/projects/${p.id}`} className="text-copper-600 hover:underline">
                    Modifier
                  </Link>
                  <AdminDeleteButton
                    apiPath={`/api/admin/projects/${p.id}`}
                    confirmMessage="Supprimer cette réalisation ?"
                  />
                </td>
              </tr>
            ))}
          </tbody>
        </table>
        {projects.length === 0 && <p className="p-6 text-graphite-900/50">Aucune réalisation.</p>}
      </div>
    </div>
  );
}
