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

  return (
    <div>
      <div className="flex items-center justify-between">
        <h1 className="font-display text-2xl font-semibold">Services</h1>
        <Link
          href={`/${locale}/admin/services/new`}
          className="rounded-full bg-copper-500 px-4 py-2 text-sm font-semibold text-graphite-950"
        >
          + Nouveau service
        </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>
            {services.map((s) => (
              <tr key={s.id} className="border-b border-graphite-800/5 last:border-0">
                <td className="px-4 py-3">{s.translations[0]?.title ?? s.slug}</td>
                <td className="px-4 py-3">{s.published ? 'Oui' : 'Non'}</td>
                <td className="px-4 py-3 text-end">
                  <Link href={`/${locale}/admin/services/${s.id}`} className="text-copper-600 hover:underline">
                    Modifier
                  </Link>
                  <AdminDeleteButton apiPath={`/api/admin/services/${s.id}`} confirmMessage="Supprimer ce service ?" />
                </td>
              </tr>
            ))}
          </tbody>
        </table>
        {services.length === 0 && <p className="p-6 text-graphite-900/50">Aucun service.</p>}
      </div>
    </div>
  );
}
