import { useState } from "react";
import { useApp } from "@/pages/app/AppContext";
import { BackHeader, cn } from "@/pages/app/components/ui";
import { merchantNotifications } from "@/mocks/dshnBusiness";

const ICONS: Record<string, { icon: string; tone: string }> = {
  order: { icon: "ri-shopping-bag-3-line", tone: "bg-accent-100 text-accent-700" },
  negotiation: { icon: "ri-chat-check-line", tone: "bg-secondary-100 text-secondary-800" },
  investment: { icon: "ri-user-received-line", tone: "bg-primary-100 text-foreground-800" },
  partnership: { icon: "ri-team-line", tone: "bg-accent-100 text-accent-700" },
  verification: { icon: "ri-verified-badge-line", tone: "bg-secondary-100 text-secondary-800" },
  system: { icon: "ri-information-line", tone: "bg-background-200 text-foreground-700" },
};

export default function BizNotifications() {
  const { goBack } = useApp();
  const [items, setItems] = useState(merchantNotifications);
  const hasUnread = items.some((n) => n.unread);

  return (
    <div className="min-h-full pb-6 bg-background-50">
      <BackHeader
        title="الإشعارات"
        onBack={goBack}
        action={
          hasUnread ? (
            <button
              type="button"
              onClick={() => setItems((list) => list.map((i) => ({ ...i, unread: false })))}
              className="text-[12.5px] font-medium text-foreground-600 hover:text-foreground-950 cursor-pointer whitespace-nowrap px-2"
            >
              تعليم الكل كمقروء
            </button>
          ) : undefined
        }
      />

      <div className="px-4 py-4 space-y-2.5">
        {items.map((n) => {
          const meta = ICONS[n.type] ?? ICONS.system;
          return (
            <button
              key={n.id}
              type="button"
              onClick={() => setItems((list) => list.map((i) => (i.id === n.id ? { ...i, unread: false } : i)))}
              className={cn(
                "w-full text-right rounded-[18px] border p-3.5 flex items-start gap-3 cursor-pointer transition-colors",
                n.unread
                  ? "bg-accent-50 border-accent-200"
                  : "bg-background-100 border-background-200 hover:border-background-300",
              )}
            >
              <div className={cn("w-10 h-10 rounded-[12px] flex items-center justify-center shrink-0", meta.tone)}>
                <i className={cn(meta.icon, "text-[19px] leading-none")} />
              </div>
              <div className="flex-1 min-w-0">
                <p className="text-[13px] text-foreground-900 leading-relaxed">{n.text}</p>
                <p className="text-[11px] text-foreground-500 mt-1">{n.time}</p>
              </div>
              {n.unread && <span className="w-2 h-2 rounded-full bg-accent-500 shrink-0 mt-1.5" />}
            </button>
          );
        })}
      </div>
    </div>
  );
}