import { useState } from "react"; import { Card, CardContent, CardHeader, CardTitle } from "./ui/card"; import { Button } from "./ui/button"; import { Badge } from "./ui/badge"; import { Switch } from "./ui/switch"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "./ui/tabs"; import { Bell, AlertTriangle, Info, Check, X, Mail, MessageSquare, Smartphone, } from "lucide-react"; import { User } from "../types"; interface NotificationCenterProps { user: User; notifications: any[]; onUpdateNotifications: (notifications: any[]) => void; } export default function NotificationCenter({ user, notifications, onUpdateNotifications, }: NotificationCenterProps) { const [filter, setFilter] = useState<"all" | "unread" | "critical">("all"); // Notification preferences const [emailEnabled, setEmailEnabled] = useState(true); const [smsEnabled, setSmsEnabled] = useState(true); const [whatsappEnabled, setWhatsappEnabled] = useState(false); const [criticalAlerts, setCriticalAlerts] = useState(true); const [inventoryAlerts, setInventoryAlerts] = useState(true); const [healthAlerts, setHealthAlerts] = useState(true); const filteredNotifications = notifications.filter((notif) => { if (filter === "unread") return !notif.read; if (filter === "critical") return notif.priority === "critical"; return true; }); const unreadCount = notifications.filter((n) => !n.read).length; const criticalCount = notifications.filter( (n) => n.priority === "critical" && !n.read, ).length; const handleMarkAsRead = (id: string) => { onUpdateNotifications( notifications.map((n) => (n.id === id ? { ...n, read: true } : n)), ); }; const handleMarkAllAsRead = () => { onUpdateNotifications(notifications.map((n) => ({ ...n, read: true }))); }; const handleDelete = (id: string) => { onUpdateNotifications(notifications.filter((n) => n.id !== id)); }; const getNotificationIcon = (type: string) => { switch (type) { case "alert": case "WARNING": case "warning": return AlertTriangle; case "CRITICAL": case "critical": return AlertTriangle; case "SUCCESS": case "success": return Check; case "INFO": case "info": return Info; default: return Bell; } }; const getNotificationColor = (type: string) => { const t = type?.toUpperCase(); switch (t) { case "CRITICAL": return "bg-red-50 border-red-200"; case "WARNING": return "bg-yellow-50 border-yellow-200"; case "SUCCESS": return "bg-green-50 border-green-200"; case "INFO": case "NORMAL": return "bg-blue-50 border-blue-200"; default: return "bg-gray-50 border-gray-200"; } }; const getPriorityBadgeColor = (priority: string) => { switch (priority) { case "critical": return "bg-red-100 text-red-800"; case "high": return "bg-orange-100 text-orange-800"; case "medium": return "bg-yellow-100 text-yellow-800"; case "low": return "bg-blue-100 text-blue-800"; default: return "bg-gray-100 text-gray-800"; } }; const parseTimestampValue = (value: unknown): Date | null => { if (value instanceof Date) { return Number.isNaN(value.getTime()) ? null : value; } if (typeof value === "number" && Number.isFinite(value)) { const normalized = Math.abs(value) < 1e12 ? value * 1000 : value; const date = new Date(normalized); return Number.isNaN(date.getTime()) ? null : date; } if (typeof value === "string" && value.trim()) { const trimmed = value.trim(); if (/^\d+(\.\d+)?$/.test(trimmed)) { const numericValue = Number(trimmed); if (Number.isFinite(numericValue)) { const normalized = Math.abs(numericValue) < 1e12 ? numericValue * 1000 : numericValue; const date = new Date(normalized); if (!Number.isNaN(date.getTime())) return date; } } const date = new Date(trimmed); return Number.isNaN(date.getTime()) ? null : date; } return null; }; const getTimeAgo = (timestamp?: unknown) => { const time = parseTimestampValue(timestamp); if (!time) return "Just now"; const diffMs = Date.now() - time.getTime(); if (diffMs <= 0) return "Just now"; const diffInMinutes = Math.floor(diffMs / (1000 * 60)); if (diffInMinutes < 1) return "Just now"; if (diffInMinutes < 60) return `${diffInMinutes}m ago`; const diffInHours = Math.floor(diffInMinutes / 60); if (diffInHours < 24) return `${diffInHours}h ago`; const diffInDays = Math.floor(diffInHours / 24); if (diffInDays < 7) return `${diffInDays}d ago`; return time.toLocaleDateString(); }; const getExactTime = (timestamp?: unknown) => { const time = parseTimestampValue(timestamp); return time ? time.toLocaleString() : ""; }; const isWaterChangeDone = (notification: any) => notification.type === "SUCCESS" || notification.type === "success" || notification.templateName === "WATER_CHANGE_DONE" || notification.subject?.toLowerCase().includes("water change completed") || notification.subject?.toLowerCase().includes("water change done"); const isFeedingCompleted = (notification: any) => notification.templateName === "FEEDING_DONE" || notification.templateName === "FEEDING_COMPLETED" || notification.subject?.toLowerCase().includes("feeding completed") || notification.subject?.toLowerCase().includes("feeding done"); const isHarvestFinished = (notification: any) => notification.templateName === "HARVEST_FINISHED" || notification.templateName === "HARVEST_DONE" || notification.subject?.toLowerCase().includes("harvest finished") || notification.subject?.toLowerCase().includes("harvest completed"); // Notifications styled as a green, "DONE" card const isCompleted = (notification: any) => isWaterChangeDone(notification) || isFeedingCompleted(notification) || isHarvestFinished(notification); const isWaterChangeTask = (notification: any) => !isCompleted(notification) && (notification.templateName === "WATER_CHANGE_TASK" || notification.subject?.toLowerCase().includes("water change task")); const isCritical = (notification: any) => notification.priority === "critical" || notification.type?.toUpperCase() === "CRITICAL" || notification.subject?.toLowerCase().includes("critical"); const isHarvestReminder = (notification: any) => !isCritical(notification) && (notification.templateName === "HARVEST_REMINDER" || notification.subject?.toLowerCase().includes("harvest reminder")); const isHarvestTask = (notification: any) => !isCompleted(notification) && (notification.templateName === "HARVEST_TASK" || notification.subject?.toLowerCase().includes("harvest task")); // Notifications styled as a blue, badge-less info card const isBlueInfo = (notification: any) => isWaterChangeTask(notification) || isHarvestReminder(notification) || isHarvestTask(notification); return (

Notification Center

Manage alerts and communication preferences

{/* Summary Cards */}
Total Notifications
{notifications.length}

{unreadCount} unread

Critical Alerts
{criticalCount}

Unread critical

Active Channels
{ [emailEnabled, smsEnabled, whatsappEnabled].filter(Boolean) .length }

Communication methods

Notifications {/* Notifications Tab */}
{filteredNotifications.map((notification) => { const Icon = getNotificationIcon(notification.type); const subjectText = notification.subject || notification.title || "Notification"; const bodyText = notification.message || notification.body || ""; return (
{notification.type?.toUpperCase() === "SUCCESS" || isCompleted(notification) ? ( ) : isCritical(notification) ? ( ) : isBlueInfo(notification) ? ( ) : notification.type?.toUpperCase() === "WARNING" ? ( ) : ( )}

{subjectText}

{isCompleted(notification) ? ( DONE ) : isBlueInfo(notification) ? null : ( {notification.priority} )} {!notification.read && (
)}
{getTimeAgo(notification.timestamp)}

{bodyText}

{!notification.read && ( )}
); })} {filteredNotifications.length === 0 && (

No notifications

{filter === "unread" ? "You're all caught up!" : filter === "critical" ? "No critical alerts at this time" : "You'll see notifications here"}

)}
{/* Preferences Tab */} Notification Channels

Choose how you want to receive alerts

Email Notifications

{user.email}

SMS Alerts

{user.phone}

WhatsApp

{user.phone}

Alert Types

Customize which alerts you want to receive

Critical System Alerts

Water quality issues, equipment failures

Inventory Alerts

Low stock, expiring items

Health & Disease Alerts

Disease detection, mortality spikes

Daily Reports

Performance summaries, growth updates

Scheduled Tasks

Feeding reminders, sampling schedules

Quiet Hours

Set times when non-critical notifications are paused

Enable Quiet Hours

10:00 PM - 7:00 AM

Note: Critical alerts will always be delivered immediately

); }