import {prisma} from "@/lib/db/prisma";

export default async function PlatformNotificationsPage() {
  const notifications = await prisma.notification.findMany({
    where: {tenantId: null},
    include: {
      user: {select: {firstName: true, lastName: true, email: true}}
    },
    orderBy: {createdAt: "desc"},
    take: 50
  });

  const statusColors: Record<string, string> = {
    QUEUED: "bg-yellow-100 text-yellow-700",
    SENT: "bg-green-100 text-green-700",
    FAILED: "bg-red-100 text-red-700",
    READ: "bg-slate-100 text-slate-600"
  };

  return (
    <div className="space-y-6 p-6">
      <div>
        <h1 className="text-2xl font-bold text-slate-900">Platform Notifications</h1>
        <p className="mt-1 text-slate-500">{notifications.length} system-level notifications</p>
      </div>

      <div className="rounded-2xl border border-slate-200 bg-white shadow-sm overflow-hidden">
        <table className="min-w-full text-sm">
          <thead>
            <tr className="border-b border-slate-200 bg-slate-50">
              <th className="px-4 py-3 text-left font-semibold text-slate-700">Type</th>
              <th className="px-4 py-3 text-left font-semibold text-slate-700">Recipient</th>
              <th className="px-4 py-3 text-left font-semibold text-slate-700">Title (EN)</th>
              <th className="px-4 py-3 text-left font-semibold text-slate-700">Status</th>
              <th className="px-4 py-3 text-left font-semibold text-slate-700">Created</th>
            </tr>
          </thead>
          <tbody className="divide-y divide-slate-100">
            {notifications.map((n) => (
              <tr key={n.id} className="hover:bg-slate-50 transition">
                <td className="px-4 py-3">
                  <span className="rounded bg-violet-50 px-2 py-0.5 text-xs font-mono text-violet-700">
                    {n.type.replace(/_/g, " ")}
                  </span>
                </td>
                <td className="px-4 py-3 text-slate-600">
                  {n.user.firstName ? `${n.user.firstName} ${n.user.lastName ?? ""}`.trim() : n.user.email}
                </td>
                <td className="px-4 py-3 text-slate-900">{n.titleEn}</td>
                <td className="px-4 py-3">
                  <span className={`rounded-full px-2 py-0.5 text-xs font-medium ${statusColors[n.status] ?? "bg-slate-100"}`}>
                    {n.status}
                  </span>
                </td>
                <td className="px-4 py-3 text-slate-500">{new Date(n.createdAt).toLocaleString()}</td>
              </tr>
            ))}
            {notifications.length === 0 && (
              <tr>
                <td className="px-4 py-8 text-center text-slate-400" colSpan={5}>
                  No platform notifications.
                </td>
              </tr>
            )}
          </tbody>
        </table>
      </div>
    </div>
  );
}
