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 AdminTestimonialsPage({ params: { locale } }: { params: { locale: Locale } }) {
  const testimonials = await prisma.testimonial.findMany({
    include: { translations: { where: { locale: 'fr' } } }
  });

  return (
    <div>
      <div className="flex items-center justify-between">
        <h1 className="font-display text-2xl font-semibold">Témoignages</h1>
        <Link
          href={`/${locale}/admin/testimonials/new`}
          className="rounded-full bg-copper-500 px-4 py-2 text-sm font-semibold text-graphite-950"
        >
          + Nouveau témoignage
        </Link>
      </div>

      <p className="mt-2 text-sm text-graphite-900/50">
        N'ajoutez que des témoignages réels, avec l'accord de leurs auteurs.
      </p>

      <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">Auteur</th>
              <th className="px-4 py-3 text-start">Extrait</th>
              <th className="px-4 py-3 text-start">Publié</th>
              <th className="px-4 py-3" />
            </tr>
          </thead>
          <tbody>
            {testimonials.map((t) => (
              <tr key={t.id} className="border-b border-graphite-800/5 last:border-0">
                <td className="px-4 py-3">
                  {t.authorName}
                  {t.companyName ? ` · ${t.companyName}` : ''}
                </td>
                <td className="max-w-xs truncate px-4 py-3 text-graphite-900/60">
                  {t.translations[0]?.quote}
                </td>
                <td className="px-4 py-3">{t.published ? 'Oui' : 'Non'}</td>
                <td className="px-4 py-3 text-end">
                  <Link href={`/${locale}/admin/testimonials/${t.id}`} className="text-copper-600 hover:underline">
                    Modifier
                  </Link>
                  <AdminDeleteButton
                    apiPath={`/api/admin/testimonials/${t.id}`}
                    confirmMessage="Supprimer ce témoignage ?"
                  />
                </td>
              </tr>
            ))}
          </tbody>
        </table>
        {testimonials.length === 0 && <p className="p-6 text-graphite-900/50">Aucun témoignage.</p>}
      </div>
    </div>
  );
}
