import { Alert, Animated, Modal, Platform, Pressable, ScrollView, Text, View, } from 'react-native'; import { useEffect, useRef, useState } from 'react'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { useRouter } from 'expo-router'; import { Ionicons } from '@expo/vector-icons'; import { useTranslation } from 'react-i18next'; import { colors } from '../lib/theme'; import { useAuthStore } from '../stores/auth'; import { useThemeStore, type ThemeMode } from '../stores/theme'; import { useLanguageStore, type AppLanguage } from '../stores/language'; import { useUserPlan } from '../hooks/useUserPlan'; import { AppHeader } from '../components/AppHeader'; // ─── Picker Sheet ────────────────────────────────────────────────────────── type PickerOption = { value: T; label: string }; function PickerSheet({ visible, title, options, selected, onSelect, onClose, }: { visible: boolean; title: string; options: PickerOption[]; selected: T; onSelect: (v: T) => void; onClose: () => void; }) { const translateY = useRef(new Animated.Value(300)).current; useEffect(() => { if (visible) { Animated.spring(translateY, { toValue: 0, useNativeDriver: true, damping: 22, stiffness: 280, }).start(); } else { Animated.timing(translateY, { toValue: 300, duration: 180, useNativeDriver: true, }).start(); } }, [visible]); return ( true} style={{ backgroundColor: '#fff', borderTopLeftRadius: 22, borderTopRightRadius: 22, paddingBottom: 34, transform: [{ translateY }], }} > {title} {options.map((opt) => ( { onSelect(opt.value); onClose(); }} style={({ pressed }) => ({ flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingHorizontal: 20, paddingVertical: 14, backgroundColor: pressed ? '#f5f5f5' : 'transparent', })} > {opt.label} {opt.value === selected && ( )} ))} ); } // ─── Settings Screen ─────────────────────────────────────────────────────── type SectionRow = { icon: React.ComponentProps['name']; iconColor: string; label: string; sublabel: string; soon?: boolean; destructive?: boolean; value?: string; onPress?: () => void; }; type Section = { key: string; title: string; rows: SectionRow[]; }; export default function SettingsScreen() { const router = useRouter(); const insets = useSafeAreaInsets(); const { t } = useTranslation(); const { signOut } = useAuthStore(); const { mode: themeMode, setMode: setThemeMode } = useThemeStore(); const { language, setLanguage } = useLanguageStore(); const { plan } = useUserPlan(); const [themePickerOpen, setThemePickerOpen] = useState(false); const [langPickerOpen, setLangPickerOpen] = useState(false); const [voicePickerOpen, setVoicePickerOpen] = useState(false); // Lyra Voice: hardcoded ElevenLabs voice IDs (expandable by user later) // Backend endpoint PATCH /api/profile/me/demographics does NOT accept lyraVoiceId. // A dedicated PATCH /api/profile/me/lyra-voice endpoint is needed from backend-agent. // For now: picker is wired to local state only, changes are NOT persisted. const [selectedVoice, setSelectedVoice] = useState('EXAVITQu4vr4xnSDxMaL'); async function handleSignOut() { Alert.alert(t('auth.signOut'), '', [ { text: t('common.cancel'), style: 'cancel' }, { text: t('auth.signOut'), style: 'cancel', onPress: async () => { await signOut(); router.replace('/'); }, }, ]); } const themeOptions: PickerOption[] = [ { value: 'system', label: t('settings.theme_system') }, { value: 'light', label: t('settings.theme_light') }, { value: 'dark', label: t('settings.theme_dark') }, ]; const themeLabel = themeMode === 'system' ? t('settings.theme_system') : themeMode === 'light' ? t('settings.theme_light') : t('settings.theme_dark'); const langOptions: PickerOption[] = [ { value: 'de', label: t('settings.language_de') }, { value: 'en', label: t('settings.language_en') }, ]; const voiceOptions: PickerOption[] = [ { value: 'EXAVITQu4vr4xnSDxMaL', label: t('settings.lyra_voice_sarah') }, { value: 'ThT5KcBeYPX3keUQqHPh', label: t('settings.lyra_voice_aria') }, { value: 'XB0fDUnXU5powFXDhCwa', label: t('settings.lyra_voice_charlotte') }, { value: 'Xb7hH8MSUJpSbSDYk0k2', label: t('settings.lyra_voice_alice') }, { value: 'pqHfZKP75CvOlQylNhV4', label: t('settings.lyra_voice_bill') }, ]; const selectedVoiceName = voiceOptions.find((v) => v.value === selectedVoice)?.label ?? t('settings.lyra_voice_sarah'); const sections: Section[] = [ { key: 'profile', title: t('settings.section_profile'), rows: [ { icon: 'person-outline', iconColor: '#6366f1', label: t('settings.profile_edit'), sublabel: t('settings.profile_edit_desc'), soon: true, }, { icon: 'image-outline', iconColor: '#6366f1', label: t('settings.profile_avatar'), sublabel: t('settings.profile_avatar_desc'), soon: true, }, ], }, { key: 'theme', title: t('settings.section_theme'), rows: [ { icon: 'color-palette-outline', iconColor: '#a78bfa', label: t('settings.theme'), sublabel: t('settings.theme_desc'), value: themeLabel, onPress: () => setThemePickerOpen(true), }, { icon: 'language-outline', iconColor: '#a78bfa', label: t('settings.language'), sublabel: t('settings.language_desc'), value: language === 'de' ? t('settings.language_de') : t('settings.language_en'), onPress: () => setLangPickerOpen(true), }, ], }, { key: 'notifications', title: t('settings.section_notifications'), rows: [ { icon: 'notifications-outline', iconColor: '#2563eb', label: t('settings.notifications_push'), sublabel: t('settings.notifications_push_desc'), soon: true, }, { icon: 'flame-outline', iconColor: '#f97316', label: t('settings.notifications_streak'), sublabel: t('settings.notifications_streak_desc'), soon: true, }, ], }, { key: 'devices', title: t('settings.section_devices'), rows: [ { icon: 'phone-portrait-outline', iconColor: '#16a34a', label: t('settings.devices'), sublabel: t('settings.devices_desc'), soon: true, }, { icon: 'star-outline', iconColor: colors.brandOrange, label: t('settings.subscription'), sublabel: t('settings.subscription_desc'), soon: true, }, ], }, { key: 'lyra', title: t('settings.section_lyra'), rows: [ { icon: 'mic-outline', iconColor: '#ec4899', label: t('settings.lyra_voice'), sublabel: plan === 'legend' ? t('settings.lyra_voice_desc') : t('settings.lyra_voice_only_legend'), value: plan === 'legend' ? selectedVoiceName : undefined, // Voice picker is wired but changes are local-only until // PATCH /api/profile/me/lyra-voice endpoint is added by backend-agent. onPress: plan === 'legend' ? () => setVoicePickerOpen(true) : undefined, soon: plan !== 'legend', }, ], }, { key: 'account', title: t('settings.danger_section'), rows: [ { icon: 'log-out-outline', iconColor: colors.textMuted, label: t('settings.sign_out'), sublabel: '', onPress: handleSignOut, }, { icon: 'trash-outline', iconColor: colors.error, label: t('settings.delete_account'), sublabel: t('settings.delete_desc'), destructive: true, soon: true, }, ], }, ]; if (__DEV__) { sections.push({ key: 'debug', title: t('settings.section_debug'), rows: [ { icon: 'bug-outline', iconColor: '#737373', label: t('settings.debug_llm'), sublabel: t('settings.debug_llm_desc'), soon: true, }, { icon: 'volume-high-outline', iconColor: '#737373', label: t('settings.debug_tts'), sublabel: t('settings.debug_tts_desc'), soon: true, }, ], }); } return ( {sections.map((section) => ( {section.title} {section.rows.map((row, i) => ( ({ flexDirection: 'row', alignItems: 'center', gap: 12, paddingHorizontal: 14, minHeight: 56, borderBottomWidth: i < section.rows.length - 1 ? 1 : 0, borderBottomColor: 'rgba(0,0,0,0.04)', opacity: row.soon ? 0.5 : pressed ? 0.7 : 1, backgroundColor: pressed && !row.soon ? '#f5f5f5' : 'transparent', })} > {row.label} {row.sublabel ? ( {row.sublabel} ) : null} {row.soon ? ( {t('settings.soon_badge')} ) : row.value ? ( {row.value} ) : ( )} ))} ))} {t('settings.skeleton_footer')} {Platform.OS} setThemePickerOpen(false)} /> setLangPickerOpen(false)} /> setVoicePickerOpen(false)} /> ); }