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 AdminFaqsPage({ params: { locale } }: { params: { locale: Locale } }) {
  const faqs = await prisma.faq.findMany({
    orderBy: { order: 'asc' },
    include: { translations: { where: { locale: 'fr' } }, service: { include: { translations: { where: { locale: 'fr' } } } } }
  });

  return (
    <div>
      <div className="flex items-center justify-between">
        <h1 className="font-display text-2xl font-semibold">FAQ</h1>
        <Link
          href={`/${locale}/admin/faqs/new`}
          className="rounded-full bg-copper-500 px-4 py-2 text-sm font-semibold text-graphite-950"
        >
          + Nouvelle question
        </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">Question</th>
              <th className="px-4 py-3 text-start">Service lié</th>
              <th className="px-4 py-3" />
            </tr>
          </thead>
          <tbody>
            {faqs.map((f) => (
              <tr key={f.id} className="border-b border-graphite-800/5 last:border-0">
                <td className="px-4 py-3">{f.translations[0]?.question}</td>
                <td className="px-4 py-3 text-graphite-900/60">
                  {f.service?.translations[0]?.title ?? '—'}
                </td>
                <td className="px-4 py-3 text-end">
                  <Link href={`/${locale}/admin/faqs/${f.id}`} className="text-copper-600 hover:underline">
                    Modifier
                  </Link>
                  <AdminDeleteButton apiPath={`/api/admin/faqs/${f.id}`} confirmMessage="Supprimer cette question ?" />
                </td>
              </tr>
            ))}
          </tbody>
        </table>
        {faqs.length === 0 && <p className="p-6 text-graphite-900/50">Aucune question.</p>}
      </div>
    </div>
  );
}
