"use client"; import { useState } from "react"; import { useMutation } from "@tanstack/react-query"; import { Activity, Bell, CheckCircle2, Clock, Loader2, XCircle, } from "lucide-react"; import { toast } from "sonner"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { useTranslations } from "@/lib/i18n/context"; import { getApiErrorMessage, useAdminSettings, useUpdateSettings, } from "./_shared"; export function RemindersSection() { const { t } = useTranslations(); const { data: settings } = useAdminSettings(); const updateSettings = useUpdateSettings(); const [reminderLateDraft, setReminderLateDraft] = useState( null, ); const [reminderMissedDraft, setReminderMissedDraft] = useState( null, ); const testNotification = useMutation({ mutationFn: async () => { const res = await fetch("/api/admin/notifications/test", { method: "POST", }); if (!res.ok) { throw new Error(await getApiErrorMessage(res)); } const json = (await res.json()) as { data?: { message?: string; results?: Array<{ channel: string; success: boolean; error?: string; }>; }; }; return json.data; }, onSuccess: (data) => { const hasFailures = data?.results?.some((r) => !r.success); if (hasFailures) { toast.error(data?.message ?? t("admin.notificationTestFailed")); } else { toast.success(data?.message ?? t("admin.notificationTestSuccess")); } }, onError: (error) => { toast.error( error instanceof Error ? error.message : t("admin.notificationTestFailed"), ); }, }); const reminderCheck = useMutation({ mutationFn: async () => { const res = await fetch("/api/admin/notifications/reminder-check", { method: "POST", }); if (!res.ok) { throw new Error(await getApiErrorMessage(res)); } const json = (await res.json()) as { data?: { message?: string; medications?: Array<{ name: string; dose: string; user: string; localTime: string; dayOfWeek: string; notificationsEnabled: boolean; schedules: Array<{ window: string; days: string; status: string; label: string; notificationSent?: boolean; }>; eventsToday: number; }>; notificationsSent?: number; }; }; return json.data; }, onSuccess: (data) => { toast.success(data?.message ?? t("admin.reminderCheckSuccess")); }, onError: (error) => { toast.error( error instanceof Error ? error.message : t("admin.reminderCheckFailed"), ); }, }); return (
{t("admin.medicationReminders")}

{t("admin.medicationRemindersDescription")}

{t("admin.reminderLateMinutesDescription")}

setReminderLateDraft(Number(e.target.value))} autoComplete="off" data-lpignore="true" data-1p-ignore="true" disabled={updateSettings.isPending} className="w-32" />

{t("admin.reminderMissedMinutesDescription")}

setReminderMissedDraft(Number(e.target.value))} autoComplete="off" data-lpignore="true" data-1p-ignore="true" disabled={updateSettings.isPending} className="w-32" />
{testNotification.data?.results && testNotification.data.results.length > 0 && (
{testNotification.data.results.map((r, i) => (
{r.success ? ( ) : ( )} {r.channel} {r.error && ( — {r.error} )}
))}
)} {reminderCheck.data?.medications && reminderCheck.data.medications.length > 0 && (

{t("admin.reminderCheckResults")}

{reminderCheck.data.medications.map((med, i) => (
{med.name} ({med.dose}) {med.notificationsEnabled ? t("admin.reminderCheckNotifOn") : t("admin.reminderCheckNotifOff")}

{med.user} — {med.dayOfWeek} {med.localTime} —{" "} {t("admin.reminderCheckEventsToday")}: {med.eventsToday}

{med.schedules.map((sched, j) => { const statusColor = sched.status === "open" ? "text-green-400" : sched.status === "threshold" ? "text-yellow-400" : sched.status === "missed" ? "text-red-400" : sched.status === "skipped" ? "text-muted-foreground" : ""; return (
{sched.window} [{sched.days}] {sched.label} {sched.notificationSent && ( {t("admin.reminderCheckNotifSent")} )}
); })}
))}
)}
); }