import type { Metadata } from "next";
import Link from "next/link";
import { requireUser } from "@/lib/auth/dal";
import { listNotifications } from "@/lib/notifications/queries";
import { markAllNotificationsRead } from "@/lib/notifications/actions";

export const metadata: Metadata = {
  title: "Bildirimler",
  robots: { index: false, follow: false },
};

function describe(notification: Awaited<ReturnType<typeof listNotifications>>[number]) {
  const payload = notification.payload as Record<string, unknown>;
  switch (notification.type) {
    case "LIKE": {
      const fromUsername = typeof payload.fromUsername === "string" ? payload.fromUsername : null;
      const fromFullName = typeof payload.fromFullName === "string" ? payload.fromFullName : "Bir üye";
      return { text: `${fromFullName} seni beğendi.`, href: fromUsername ? `/profil/${fromUsername}` : null };
    }
    case "MESSAGE": {
      const fromFullName = typeof payload.fromFullName === "string" ? payload.fromFullName : "Bir üye";
      const conversationId = typeof payload.conversationId === "string" ? payload.conversationId : null;
      return { text: `${fromFullName} sana mesaj gönderdi.`, href: conversationId ? `/mesajlar/${conversationId}` : null };
    }
    case "TAG": {
      const fromFullName = typeof payload.fromFullName === "string" ? payload.fromFullName : "Bir üye";
      return { text: `${fromFullName} seni bir itirafta etiketledi.`, href: "/itiraflar" };
    }
    case "QUOTE_RECEIVED": {
      const fromFullName = typeof payload.fromFullName === "string" ? payload.fromFullName : "Bir üye";
      return { text: `${fromFullName} sana bir güzel söz gönderdi.`, href: "/sozler" };
    }
    case "COMMENT": {
      const fromFullName = typeof payload.fromFullName === "string" ? payload.fromFullName : "Bir üye";
      const confessionSlug = typeof payload.confessionSlug === "string" ? payload.confessionSlug : null;
      return {
        text: `${fromFullName} bir itirafta sana yorum/yanıt bıraktı.`,
        href: confessionSlug ? `/itiraflar/${confessionSlug}` : "/itiraflar",
      };
    }
    case "QUOTE_COMMENT": {
      const fromFullName = typeof payload.fromFullName === "string" ? payload.fromFullName : "Bir üye";
      const quoteSlug = typeof payload.quoteSlug === "string" ? payload.quoteSlug : null;
      return {
        text: `${fromFullName} bir sözde sana yanıt bıraktı.`,
        href: quoteSlug ? `/sozler/soz/${quoteSlug}` : "/sozler",
      };
    }
    default:
      return { text: "Yeni bir bildirimin var.", href: null };
  }
}

export default async function NotificationsPage() {
  const user = await requireUser();
  const notifications = await listNotifications(user.id);

  return (
    <div className="mx-auto max-w-2xl px-6 py-14">
      <div className="mb-6 flex items-center justify-between">
        <h1 className="text-[26px]">Bildirimler</h1>
        {notifications.some((n) => !n.isRead) && (
          <form action={markAllNotificationsRead}>
            <button className="text-[13px] font-semibold text-accent">Tümünü okundu işaretle</button>
          </form>
        )}
      </div>

      {notifications.length === 0 ? (
        <p className="text-[13.5px] text-text-muted">Henüz bildirimin yok.</p>
      ) : (
        <div className="flex flex-col gap-2">
          {notifications.map((notification) => {
            const { text, href } = describe(notification);
            const content = (
              <div
                className={`rounded-xl border px-4 py-3 text-[14px] ${
                  notification.isRead ? "border-border bg-surface text-text-muted" : "border-accent/30 bg-accent/5 text-text"
                }`}
              >
                {text}
              </div>
            );
            return href ? (
              <Link key={notification.id} href={href}>
                {content}
              </Link>
            ) : (
              <div key={notification.id}>{content}</div>
            );
          })}
        </div>
      )}
    </div>
  );
}
