import React, { useState, useEffect, useCallback } from 'react'; import { View, Text, StyleSheet, ScrollView, TouchableOpacity, RefreshControl, } from 'react-native'; import { useLocalSearchParams, router } from 'expo-router'; import { Ionicons } from '@expo/vector-icons'; import { SafeAreaView } from 'react-native-safe-area-context'; import { api } from '@/services/api'; import { useBeneficiary } from '@/contexts/BeneficiaryContext'; import { LoadingSpinner } from '@/components/ui/LoadingSpinner'; import { FullScreenError } from '@/components/ui/ErrorMessage'; import { Button } from '@/components/ui/Button'; import { AppColors, BorderRadius, FontSizes, Spacing } from '@/constants/theme'; import type { Beneficiary } from '@/types'; export default function BeneficiaryDetailScreen() { const { id } = useLocalSearchParams<{ id: string }>(); const { setCurrentBeneficiary } = useBeneficiary(); const [beneficiary, setBeneficiary] = useState(null); const [isLoading, setIsLoading] = useState(true); const [isRefreshing, setIsRefreshing] = useState(false); const [error, setError] = useState(null); const loadBeneficiary = useCallback(async (showLoading = true) => { if (!id) return; if (showLoading) setIsLoading(true); setError(null); try { const response = await api.getBeneficiary(parseInt(id, 10)); if (response.ok && response.data) { setBeneficiary(response.data); } else { setError(response.error?.message || 'Failed to load beneficiary'); } } catch (err) { setError(err instanceof Error ? err.message : 'An error occurred'); } finally { setIsLoading(false); setIsRefreshing(false); } }, [id]); useEffect(() => { loadBeneficiary(); }, [loadBeneficiary]); const handleRefresh = useCallback(() => { setIsRefreshing(true); loadBeneficiary(false); }, [loadBeneficiary]); const handleChatPress = () => { // Set current beneficiary in context before navigating to chat // This allows the chat to include beneficiary context in AI questions if (beneficiary) { setCurrentBeneficiary(beneficiary); } router.push('/(tabs)/chat'); }; if (isLoading) { return ; } if (error || !beneficiary) { return ( loadBeneficiary()} /> ); } return ( {/* Header */} router.back()} > {beneficiary.name} } > {/* Beneficiary Info Card */} {beneficiary.name.charAt(0).toUpperCase()} {beneficiary.status === 'online' ? 'Online' : 'Offline'} {beneficiary.name} {beneficiary.address && ( {beneficiary.address} )} {beneficiary.last_location ? `📍 ${beneficiary.last_location}` : ''} • {beneficiary.last_activity || 'No recent activity'} {/* Sensor Stats - using real API data */} Sensor Overview {beneficiary.status === 'online' ? 'Active' : 'Inactive'} Status {beneficiary.last_activity || '--'} {beneficiary.last_location || '--'} Location {beneficiary.before_last_location || '--'} {beneficiary.temperature?.toFixed(1) || '--'}{beneficiary.units || '°F'} Temperature Bedroom: {beneficiary.bedroom_temperature?.toFixed(1) || '--'}{beneficiary.units || '°F'} {/* Additional stats row */} {beneficiary.sleep_hours?.toFixed(1) || '--'}h Sleep = 70 ? '#D1FAE5' : '#FEF3C7' }]}> = 70 ? AppColors.success : AppColors.warning} /> {beneficiary.wellness_score || '--'}% Wellness {beneficiary.wellness_descriptor || '--'} {beneficiary.address?.split(',')[0] || '--'} Address {/* Quick Actions */} Quick Actions Chat with Julia Set Reminder Video Call Activity Report {/* Chat with Julia Button */}