import React, { useState } from 'react'; import { View, Text, StyleSheet, TextInput, TouchableOpacity, Alert, ActivityIndicator, ScrollView, KeyboardAvoidingView, Platform, } from 'react-native'; import { Ionicons } from '@expo/vector-icons'; import { SafeAreaView } from 'react-native-safe-area-context'; import { router, useLocalSearchParams } from 'expo-router'; import { useBeneficiary } from '@/contexts/BeneficiaryContext'; import { AppColors, BorderRadius, FontSizes, FontWeights, Spacing, Shadows, } from '@/constants/theme'; type Role = 'caretaker' | 'guardian'; export default function ShareAccessScreen() { const { id } = useLocalSearchParams<{ id: string }>(); const { currentBeneficiary } = useBeneficiary(); const [email, setEmail] = useState(''); const [label, setLabel] = useState(''); const [role, setRole] = useState('caretaker'); const [isLoading, setIsLoading] = useState(false); const beneficiaryName = currentBeneficiary?.name || 'this person'; const validateEmail = (email: string): boolean => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); }; const handleSendInvite = async () => { const trimmedEmail = email.trim().toLowerCase(); const trimmedLabel = label.trim(); if (!trimmedEmail) { Alert.alert('Email Required', 'Please enter an email address.'); return; } if (!validateEmail(trimmedEmail)) { Alert.alert('Invalid Email', 'Please enter a valid email address.'); return; } setIsLoading(true); try { // TODO: Send invitation via API // await api.sendInvitation({ // beneficiaryId: id, // email: trimmedEmail, // label: trimmedLabel, // role: role, // }); // For now, show success message Alert.alert( 'Invitation Sent', `An invitation has been sent to ${trimmedEmail} to ${role === 'guardian' ? 'manage' : 'view'} ${beneficiaryName}.`, [{ text: 'OK', onPress: () => router.back() }] ); } catch (error) { console.error('Failed to send invitation:', error); Alert.alert('Error', 'Failed to send invitation. Please try again.'); } finally { setIsLoading(false); } }; return ( {/* Header */} router.back()}> Share Access {/* Info Banner */} Invite family members or caregivers to help monitor {beneficiaryName} {/* Email Input */} Email Address {/* Label Input */} Label (optional) {/* Role Selection */} Access Level setRole('caretaker')} disabled={isLoading} > Caretaker Can view activity and chat with Julia {role === 'caretaker' && ( )} setRole('guardian')} disabled={isLoading} > Guardian Full access: edit info, manage subscription, invite others {role === 'guardian' && ( )} {/* Send Button */} {isLoading ? ( ) : ( <> Send Invitation )} {/* Help Text */} The person will receive an email with instructions to access {beneficiaryName}'s information. ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: AppColors.background, }, header: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingHorizontal: Spacing.md, paddingVertical: Spacing.sm, borderBottomWidth: 1, borderBottomColor: AppColors.border, }, backButton: { padding: Spacing.xs, }, headerTitle: { fontSize: FontSizes.lg, fontWeight: FontWeights.semibold, color: AppColors.textPrimary, }, placeholder: { width: 32, }, content: { flex: 1, }, scrollContent: { padding: Spacing.lg, paddingBottom: Spacing.xxl, }, infoBanner: { flexDirection: 'row', alignItems: 'center', backgroundColor: AppColors.primaryLighter, padding: Spacing.md, borderRadius: BorderRadius.lg, marginBottom: Spacing.xl, gap: Spacing.md, }, infoBannerText: { flex: 1, fontSize: FontSizes.sm, color: AppColors.textPrimary, lineHeight: 20, }, inputGroup: { marginBottom: Spacing.lg, }, inputLabel: { fontSize: FontSizes.sm, fontWeight: FontWeights.medium, color: AppColors.textSecondary, marginBottom: Spacing.sm, }, inputContainer: { flexDirection: 'row', alignItems: 'center', backgroundColor: AppColors.surface, borderRadius: BorderRadius.lg, paddingHorizontal: Spacing.md, borderWidth: 1, borderColor: AppColors.border, }, input: { flex: 1, paddingVertical: Spacing.md, marginLeft: Spacing.sm, fontSize: FontSizes.base, color: AppColors.textPrimary, }, roleOption: { backgroundColor: AppColors.surface, borderRadius: BorderRadius.lg, padding: Spacing.md, marginBottom: Spacing.sm, borderWidth: 2, borderColor: AppColors.border, }, roleOptionSelected: { borderColor: AppColors.primary, backgroundColor: AppColors.primaryLighter, }, roleHeader: { flexDirection: 'row', alignItems: 'center', }, roleIcon: { width: 40, height: 40, borderRadius: BorderRadius.md, backgroundColor: AppColors.surfaceSecondary, justifyContent: 'center', alignItems: 'center', marginRight: Spacing.md, }, roleIconSelected: { backgroundColor: AppColors.primary, }, roleInfo: { flex: 1, }, roleTitle: { fontSize: FontSizes.base, fontWeight: FontWeights.semibold, color: AppColors.textPrimary, }, roleTitleSelected: { color: AppColors.primary, }, roleDescription: { fontSize: FontSizes.sm, color: AppColors.textSecondary, marginTop: 2, }, sendButton: { flexDirection: 'row', alignItems: 'center', justifyContent: 'center', backgroundColor: AppColors.primary, paddingVertical: Spacing.md, borderRadius: BorderRadius.lg, marginTop: Spacing.lg, gap: Spacing.sm, ...Shadows.primary, }, sendButtonDisabled: { opacity: 0.7, }, sendButtonText: { fontSize: FontSizes.base, fontWeight: FontWeights.semibold, color: AppColors.white, }, helpText: { fontSize: FontSizes.sm, color: AppColors.textMuted, textAlign: 'center', marginTop: Spacing.lg, lineHeight: 20, }, });