import { useLocalSearchParams } from "expo-router"; import { useMemo, useState } from "react"; import { Alert, ScrollView, View } from "react-native"; import { Badge } from "@/components/ui/Badge"; import { Button } from "@/components/ui/Button"; import { Card } from "@/components/ui/Card"; import { Chip } from "@/components/ui/Chip"; import { Field, SwitchRow } from "@/components/ui/Field"; import { AppHeader } from "@/components/ui/Header"; import { EmptyState } from "@/components/ui/EmptyState"; import { Screen } from "@/components/ui/Screen"; import { Text } from "@/components/ui/Text"; import { scheduleDoseReminder, scheduleEscalationCheck, } from "@/lib/notifications"; import { useEmergencyContactStore } from "@/lib/stores/emergencyContactStore"; import { usePrescriptionStore } from "@/lib/stores/prescriptionStore"; import { colors, gutter } from "@/lib/theme"; const PRESETS = ["08:00", "12:00", "14:00", "18:00", "20:00"] as const; export default function RxCalendarScreen() { const { id } = useLocalSearchParams<{ id: string }>(); const rx = usePrescriptionStore((s) => s.prescriptions.find((p) => p.id === id)); const upsertReminder = usePrescriptionStore((s) => s.upsertReminder); const schedulePendingDose = usePrescriptionStore((s) => s.schedulePendingDose); const confirmDose = usePrescriptionStore((s) => s.confirmDose); const escalateDose = usePrescriptionStore((s) => s.escalateDose); const doseLogs = usePrescriptionStore((s) => s.doseLogs); const reminder = usePrescriptionStore((s) => s.reminders.find((r) => r.prescriptionId === id) ); const grace = useEmergencyContactStore((s) => s.graceMinutesDefault); const setGrace = useEmergencyContactStore((s) => s.setGrace); const escalationEnabled = useEmergencyContactStore((s) => s.escalationEnabled); const setEscalationEnabled = useEmergencyContactStore((s) => s.setEscalationEnabled); const contacts = useEmergencyContactStore((s) => s.contacts); const markEscalated = useEmergencyContactStore((s) => s.markEscalated); const [times, setTimes] = useState(reminder?.times.join(",") || "08:00,20:00"); const [pendingLogId, setPendingLogId] = useState(null); const reminderId = `rem_${id}`; const relatedLogs = useMemo( () => doseLogs.filter((l) => l.reminderId === reminderId).slice(0, 8), [doseLogs, reminderId] ); if (!rx) { return ( ); } const togglePreset = (t: string) => { const list = times .split(",") .map((s) => s.trim()) .filter(Boolean); if (list.includes(t)) { setTimes(list.filter((x) => x !== t).join(",")); } else { setTimes([...list, t].sort().join(",")); } }; const saveAndSchedule = async () => { const parsed = times .split(",") .map((s) => s.trim()) .filter(Boolean); if (!parsed.length) { Alert.alert("Times required", "Pick at least one reminder time."); return; } upsertReminder({ id: reminderId, prescriptionId: rx.id, medicineName: rx.medicineName, times: parsed, daysOfWeek: [0, 1, 2, 3, 4, 5, 6], graceMinutes: grace, escalateOnMiss: escalationEnabled, enabled: true, }); await scheduleDoseReminder({ title: `Time for ${rx.medicineName}`, body: "Tap to confirm you took your dose", secondsFromNow: 15, data: { prescriptionId: rx.id, type: "dose" }, }); const log = schedulePendingDose(reminderId); setPendingLogId(log.id); if (escalationEnabled && contacts.length) { await scheduleEscalationCheck({ title: "Dose not confirmed", body: `Notifying ${contacts[0].name} if still unconfirmed`, graceSeconds: Math.max(20, grace * 60), data: { logId: log.id, type: "escalation" }, }); setTimeout(() => { const current = usePrescriptionStore .getState() .doseLogs.find((l) => l.id === log.id); if (current?.status === "pending") { escalateDose(log.id); markEscalated(); Alert.alert("Emergency", `${contacts[0].name} would be notified (mock SMS/push)`); } }, Math.max(25000, grace * 1000)); } else if (escalationEnabled && !contacts.length) { Alert.alert( "No emergency contact", "Add a contact under Profile → Emergency contacts for escalation." ); } Alert.alert("Scheduled", `Reminders at ${parsed.join(", ")}. Demo push in ~15s.`); }; return ( Patient {rx.patientName || "Self"} {rx.status} · {rx.daysSupply} day supply Dose times Tap presets or edit the comma-separated list {PRESETS.map((p) => { const selected = times .split(",") .map((s) => s.trim()) .includes(p); return ( togglePreset(p)} size="sm" /> ); })} setGrace(Number(v) || 30)} /> {pendingLogId ? ( Confirm this dose Demo reminder is active. Mark taken or snooze.