import React, { useState } from 'react';
import {
ActivityIndicator,
Image,
Alert,
Linking,
Pressable,
ScrollView,
StyleSheet,
Text,
View,
} from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import { useRouter } from 'expo-router';
import { useQuery } from '@tanstack/react-query';
import { api, USE_MOCK } from '@/api';
import { AppBar } from '@/components/AppBar';
import { Avatar, Badge, Button, Card, SectionTitle } from '@/components/ui';
import { useAuth } from '@/auth/AuthContext';
import { LanguagePreference, useI18n } from '@/i18n';
import { colors, fontSize, radii, spacing } from '@/theme';
// Both stores check these open a real page before accepting a build.
const PRIVACY_URL = 'https://casadoc.app/privacy';
const TERMS_URL = 'https://casadoc.app/terms';
export default function ProfileScreen() {
const { user, signOut, deleteAccount } = useAuth();
const router = useRouter();
const { t, preference, setPreference } = useI18n();
const [signingOut, setSigningOut] = useState(false);
const [deleting, setDeleting] = useState(false);
const propsQuery = useQuery({ queryKey: ['properties'], queryFn: () => api.properties.list() });
const fullName = [user?.firstName, user?.lastName].filter(Boolean).join(' ') || user?.email || '—';
const initials = ((user?.firstName?.[0] ?? '') + (user?.lastName?.[0] ?? '')).toUpperCase() || 'U';
const languageOptions: { value: LanguagePreference; label: string; sub?: string }[] = [
{ value: 'system', label: t('profile.languageSystem'), sub: t('profile.languageSystemSub') },
{ value: 'en', label: 'English' },
{ value: 'it', label: 'Italiano' },
];
// Both stores require in-app deletion, with the consequences spelled out.
function confirmDelete() {
Alert.alert(t('profile.deleteConfirmTitle'), t('profile.deleteConfirmBody'), [
{ text: t('common.cancel'), style: 'cancel' },
{
text: t('profile.deleteConfirmCta'),
style: 'destructive',
onPress: async () => {
setDeleting(true);
try {
await deleteAccount();
} catch (e: any) {
Alert.alert(t('profile.deleteFailed'), e?.message ?? t('common.tryAgain'));
} finally {
setDeleting(false);
}
},
},
]);
}
async function onSignOut() {
setSigningOut(true);
try {
await signOut();
} finally {
setSigningOut(false);
}
}
return (
{user?.avatar ? (
) : (
)}
{fullName}
{user?.email}
{/* Language */}
{languageOptions.map((opt, i) => {
const selected = preference === opt.value;
return (
{i > 0 ? : null}
setPreference(opt.value)}
accessibilityRole="radio"
accessibilityState={{ selected }}
>
{opt.label}
{opt.sub ? {opt.sub} : null}
{selected ? : null}
);
})}
{/* Account */}
router.push('/profile/edit')}>
{t('profile.personalDetails')}
{t('profile.personalDetailsSub')}
router.push('/profile/password')}>
{t('profile.security')}
{t('profile.securitySub')}
{/* Legal — store listings require these to be reachable. */}
Linking.openURL(PRIVACY_URL)}>
{t('profile.privacy')}
Linking.openURL(TERMS_URL)}>
{t('profile.terms')}
{USE_MOCK ? {t('profile.demoMode')} : null}
{t('profile.deleteAccount')}
{t('profile.deleteAccountSub')}
{deleting ? : null}
);
}
function Divider() {
return ;
}
const styles = StyleSheet.create({
body: { padding: spacing[4], paddingBottom: spacing[9] },
heroAvatar: { width: 60, height: 60, borderRadius: 30, backgroundColor: colors.surfaceSunken },
hero: { flexDirection: 'row', alignItems: 'center', gap: spacing[4] },
name: { fontSize: fontSize.lg, fontWeight: '700', color: colors.ink },
email: { fontSize: fontSize.sm, color: colors.textMuted, marginTop: 2 },
row: { flexDirection: 'row', alignItems: 'center', gap: spacing[3], padding: spacing[4] },
rowIcon: {
width: 38,
height: 38,
borderRadius: 10,
backgroundColor: colors.teal50,
alignItems: 'center',
justifyContent: 'center',
},
rowTitle: { fontSize: fontSize.base, fontWeight: '600', color: colors.ink },
rowSub: { fontSize: fontSize.xs, color: colors.textMuted, marginTop: 2 },
divider: { height: 1, backgroundColor: colors.border, marginLeft: 58 },
radio: {
width: 22,
height: 22,
borderRadius: 11,
borderWidth: 2,
borderColor: colors.borderStrong,
alignItems: 'center',
justifyContent: 'center',
},
radioOn: { borderColor: colors.teal600 },
radioDot: { width: 10, height: 10, borderRadius: 5, backgroundColor: colors.teal600 },
dangerRow: {
flexDirection: 'row',
alignItems: 'center',
gap: spacing[3],
padding: spacing[4],
backgroundColor: colors.surface,
borderWidth: 1,
borderColor: '#f0c2c9',
borderRadius: radii.lg,
},
dangerIcon: {
width: 38,
height: 38,
borderRadius: 10,
backgroundColor: colors.dangerBg,
alignItems: 'center',
justifyContent: 'center',
},
dangerTitle: { fontSize: fontSize.base, fontWeight: '700', color: colors.danger600 },
demo: { textAlign: 'center', color: colors.textMuted2, fontSize: fontSize.xs, marginTop: spacing[5] },
});