import { redirect } from "next/navigation";

import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { listNotifications } from "@/lib/appwrite/notification-helpers";
import { requireTenant } from "@/lib/appwrite/tenant-guard";
import { NotificationsList } from "./components/notifications-list";

export const metadata = {
  title: "DLS — Bildirimler",
};

export default async function NotificationsPage() {
  let ctx;
  try {
    ctx = await requireTenant();
  } catch {
    redirect("/onboarding");
  }

  const rows = await listNotifications(ctx.tenantId);

  return (
    <div className="flex-1 space-y-6 px-6">
      <div className="flex flex-col gap-1">
        <h1 className="text-2xl font-bold tracking-tight">Bildirimler</h1>
        <p className="text-muted-foreground text-sm">
          Bağlantı talepleri, iş güncellemeleri ve diğer olaylar.
        </p>
      </div>

      <Card>
        <CardHeader>
          <CardTitle>Son Bildirimler</CardTitle>
          <CardDescription>
            {rows.length === 0 ? "Henüz bildirim yok." : `${rows.length} kayıt`}
          </CardDescription>
        </CardHeader>
        <CardContent>
          <NotificationsList rows={rows} />
        </CardContent>
      </Card>
    </div>
  );
}
