import { useAlert } from '@/aesthetics/Alert'; import { useStrings } from '@/common/hooks/useStrings'; import { useAuth } from '@/profile/auth/context'; import { usePreferences } from '@/profile/preferences'; import { useProfile } from '@/profile/ProfileProvider'; import { ProfileRepository } from '@/profile/repository'; import { useDatabase } from '@/remote/database'; import type { UserProfile } from '@/types/foodlog'; import { useRouter } from 'expo-router'; import { useCallback, useMemo, useState } from 'react'; import { Linking } from 'react-native'; export const getBiologicalSexOptions = (strings: ReturnType) => [ { value: null, label: strings.common.notSet }, { value: 'M', label: strings.profile.male }, { value: 'F', label: strings.profile.female }, ] as const; export const useProfileSettings = () => { const { isDarkMode, setTheme, hapticFeedback, setHapticFeedback, languagePreference, setLanguage, } = usePreferences(); const { user, signOut } = useAuth(); const { profile } = useProfile(); const db = useDatabase(); const profileRepo = useMemo(() => new ProfileRepository(db), [db]); const router = useRouter(); const strings = useStrings(); const { alert } = useAlert(); const [editingField, setEditingField] = useState(null); const [tempValue, setTempValue] = useState(''); const isDemo = !user; const updateUserProfile = useCallback( async ( updates: Partial< Omit > ) => { if (!profile || !user) return false; try { await profileRepo.updateProfile(profile.id, user.id, updates); return true; } catch (e) { console.error('Error updating profile:', e); return false; } }, [profile, user, profileRepo] ); const handleSignOut = () => { alert(strings.auth.signOutConfirmTitle, strings.auth.signOutConfirmMessage, [ { text: strings.common.cancel, style: 'cancel' }, { text: strings.auth.signOut, style: 'destructive', onPress: signOut }, ]); }; const startEditing = (field: string, currentValue: string | number | null) => { setEditingField(field); setTempValue(currentValue?.toString() || ''); }; const saveField = async (field: string) => { setEditingField(null); const numValue = tempValue ? parseInt(tempValue, 10) : null; if (field === 'birth_year' && numValue !== profile?.birth_year) { await updateUserProfile({ birth_year: numValue }); } else if (field === 'body_height_cm' && numValue !== profile?.body_height_cm) { await updateUserProfile({ body_height_cm: numValue }); } else if (field === 'body_weight_kg' && numValue !== profile?.body_weight_kg) { await updateUserProfile({ body_weight_kg: numValue }); } }; const updateSex = async (value: 'M' | 'F' | null) => { await updateUserProfile({ biological_sex: value }); }; const handleOpenPrivacy = () => Linking.openURL('https://www.junwon.com/privacy-policy'); const handleOpenTerms = () => Linking.openURL('https://www.junwon.com/terms-of-service'); const handleContactSupport = () => Linking.openURL('mailto:junwon@palacelog.com'); const handleNavigateToBoard = () => router.push('/board'); const handleNavigateToCastle = () => router.push('/castle'); const handleNavigateToSignIn = () => router.push('/signin'); return { isDarkMode, hapticFeedback, user, profile, isDemo, languagePreference, strings, editingField, tempValue, setTheme, setHapticFeedback, setTempValue, handleSignOut, startEditing, saveField, updateSex, setLanguage, handleOpenPrivacy, handleOpenTerms, handleContactSupport, handleNavigateToBoard, handleNavigateToCastle, handleNavigateToSignIn, }; };