import { prisma } from '@/lib/prisma';
import DeleteCommentButton from '@/components/DeleteCommentButton';

async function getComments() {
  return await prisma.comment.findMany({
    include: {
      author: true,
      business: true,
    },
    orderBy: {
      createdAt: 'desc',
    },
  });
}

export default async function CommentsPage() {
  const comments = await getComments();

  return (
    <div>
      <div className="mb-8">
        <h1 className="text-3xl font-bold text-white mb-2">Modération des Commentaires</h1>
        <p className="text-slate-400">Surveiller et supprimer les commentaires inappropriés.</p>
      </div>

      <div className="card overflow-hidden p-0">
        <table className="data-table">
          <thead>
            <tr>
              <th>Auteur</th>
              <th>Entreprise</th>
              <th>Contenu</th>
              <th>Date</th>
              <th className="text-right">Action</th>
            </tr>
          </thead>
          <tbody>
            {comments.length === 0 ? (
              <tr>
                <td colSpan={5} className="text-center py-10 text-slate-500">
                  Aucun commentaire trouvé.
                </td>
              </tr>
            ) : (
              comments.map((comment) => (
                <tr key={comment.id}>
                  <td>
                    <div className="font-medium text-white">{comment.author.name}</div>
                    <div className="text-xs text-slate-500">{comment.author.email}</div>
                  </td>
                  <td>
                    <div className="text-sm text-indigo-400">{comment.business.name}</div>
                  </td>
                  <td className="max-w-md">
                    <p className="text-sm text-slate-300 truncate" title={comment.content}>
                      {comment.content}
                    </p>
                  </td>
                  <td>
                    <div className="text-sm text-slate-500">
                      {new Date(comment.createdAt).toLocaleDateString('fr-FR')}
                    </div>
                  </td>
                  <td className="text-right">
                    <DeleteCommentButton id={comment.id} />
                  </td>
                </tr>
              ))
            )}
          </tbody>
        </table>
      </div>
    </div>
  );
}
