"use client";

import Link from "next/link";
import { Check, ExternalLink, Trash2 } from "lucide-react";
import { Badge } from "@/components/ui/badge";
import { Card, CardContent } from "@/components/ui/card";
import { ActionButton } from "@/components/ui/action-button";
import { cn } from "@/lib/utils";
import {
  deleteNotificationAction,
  markNotificationReadAction,
} from "@/actions/notifications";
import { NOTIFICATION_TONE } from "@/lib/labels";
import { formatRelativeTime } from "@/lib/format";
import type { NotificationRow } from "@/lib/queries/notifications";

/** Tone → dot background class, kept beside the row that renders it. */
const DOT_CLASS: Record<string, string> = {
  info: "bg-info-500",
  success: "bg-success-500",
  warning: "bg-warning-500",
  danger: "bg-danger-500",
  brand: "bg-brand-500",
  neutral: "bg-tertiary",
  outline: "bg-tertiary",
};

/** A single inbox row with a tone dot, body, optional link and actions. */
export function NotificationItem({ notification }: { notification: NotificationRow }) {
  const tone = NOTIFICATION_TONE[notification.type];
  const unread = notification.readAt === null;

  return (
    <Card className={cn(unread && "border-brand-200 dark:border-brand-800")}>
      <CardContent className="flex items-start gap-3.5 pt-5">
        <span
          className={cn("mt-1.5 size-2.5 shrink-0 rounded-full", DOT_CLASS[tone])}
          aria-hidden="true"
        />

        <div className="min-w-0 flex-1 space-y-1">
          <div className="flex flex-wrap items-center gap-2">
            <p
              className={cn(
                "text-sm",
                unread ? "font-semibold text-primary" : "font-medium text-secondary"
              )}
            >
              {notification.title}
            </p>
            {unread ? <Badge variant="brand">Baru</Badge> : null}
          </div>

          {notification.body ? (
            <p className="text-sm leading-relaxed text-secondary">{notification.body}</p>
          ) : null}

          <div className="flex flex-wrap items-center gap-x-3 gap-y-1 pt-0.5">
            <span className="text-xs text-tertiary">
              {formatRelativeTime(notification.createdAt)}
            </span>
            {notification.link ? (
              <Link
                href={notification.link}
                className="inline-flex items-center gap-1 text-xs font-medium text-brand-600 hover:underline dark:text-brand-200"
              >
                Buka
                <ExternalLink className="size-3" />
              </Link>
            ) : null}
          </div>
        </div>

        <div className="flex shrink-0 items-center gap-1">
          {unread ? (
            <ActionButton
              action={markNotificationReadAction}
              fields={{ notificationId: notification.id }}
              label="Tandai dibaca"
              icon={<Check className="size-3.5" />}
              variant="ghost"
              size="sm"
            />
          ) : null}

          <ActionButton
            action={deleteNotificationAction}
            fields={{ notificationId: notification.id }}
            label=""
            icon={<Trash2 className="size-3.5" />}
            variant="ghost"
            size="icon-sm"
            confirm="Notifikasi ini akan dihapus dari kotak masukmu."
            confirmTitle="Hapus notifikasi?"
            confirmLabel="Hapus"
          />
        </div>
      </CardContent>
    </Card>
  );
}
