import React, { useState, useEffect, useRef } from "react"; import { ScrollView, View, Image, TouchableOpacity } from "react-native"; import { Button } from "~/components/ui/button"; import { Text } from "~/components/ui/text"; import { router } from "expo-router"; import { useAuthWithProfile } from "~/lib/hooks/useAuthWithProfile"; import { ROUTES } from "~/lib/routes"; import ScreenWrapper from "~/components/ui/ScreenWrapper"; import { Icons } from "~/assets/icons"; import ModalToast from "~/components/ui/toast"; import { useTranslation } from "react-i18next"; import { ChevronRight, Store, LifeBuoy, Bell, ScanFace, Grid3x3, LogOut, Book, Award, Settings, } from "lucide-react-native"; import Toggle from "~/components/ui/toggle"; import BottomSheet from "~/components/ui/bottomSheet"; import { useLangStore } from "~/lib/stores"; import { getPointsState } from "~/lib/services/pointsService"; import BackButton from "~/components/ui/backButton"; export default function Profile() { const { t } = useTranslation(); const { signOut, user, profile, profileLoading } = useAuthWithProfile(); const language = useLangStore((state) => state.language); const setLanguage = useLangStore((state) => state.setLanguage); // Preferences state const [pushNotifications, setPushNotifications] = useState(true); const [smsNotifications, setSmsNotifications] = useState(true); const [emailNotifications, setEmailNotifications] = useState(true); const [faceID, setFaceID] = useState(true); const [profileImage, setProfileImage] = useState(null); const [languageSheetVisible, setLanguageSheetVisible] = useState(false); const [pointsTotal, setPointsTotal] = useState(null); const [toastVisible, setToastVisible] = useState(false); const [toastTitle, setToastTitle] = useState(""); const [toastDescription, setToastDescription] = useState( undefined ); const [toastVariant, setToastVariant] = useState< "success" | "error" | "warning" | "info" >("info"); const toastTimeoutRef = useRef | null>(null); const showToast = ( title: string, description?: string, variant: "success" | "error" | "warning" | "info" = "info" ) => { if (toastTimeoutRef.current) { clearTimeout(toastTimeoutRef.current); } setToastTitle(title); setToastDescription(description); setToastVariant(variant); setToastVisible(true); toastTimeoutRef.current = setTimeout(() => { setToastVisible(false); toastTimeoutRef.current = null; }, 2500); }; useEffect(() => { return () => { if (toastTimeoutRef.current) { clearTimeout(toastTimeoutRef.current); } }; }, []); useEffect(() => { if (profile?.photoUrl) { setProfileImage(profile.photoUrl); } else { setProfileImage(null); } }, [profile?.photoUrl]); useEffect(() => { (async () => { try { const state = await getPointsState(); setPointsTotal(state.total); } catch (error) { if (__DEV__) { console.warn("[Profile] Failed to load points state", error); } } })(); }, []); const handleLogout = async () => { try { await signOut(); showToast( t("profile.toastLoggedOutTitle"), t("profile.toastLoggedOutDescription"), "success" ); router.replace(ROUTES.SIGNIN); } catch (error) { console.log(error); showToast( t("profile.toastErrorTitle"), t("profile.toastLogoutFailed"), "error" ); } }; const handleEditProfile = () => { router.push(ROUTES.EDIT_PROFILE); }; const handleMyStores = () => { // TODO: Navigate to My Stores screen showToast("My stores", "Coming soon", "info"); }; const handleSupport = () => { router.push(ROUTES.HELP_SUPPORT); }; const handleKyc = () => { router.push(ROUTES.KYC); }; const handlePoints = () => { router.push(ROUTES.POINTS); }; const handleHistory = () => { router.push(ROUTES.HISTORY); }; const handleChangePassword = () => { // Placeholder screen for Change Password showToast("Change Password", "Coming soon", "info"); }; const handlePINCode = () => { router.push(ROUTES.CHANGE_PIN); }; const displayName = profile?.fullName || user?.displayName || "User"; const displayEmail = profile?.email || user?.email || ""; const agentId = user?.uid ? `AGENT-${user.uid.slice(-6).toUpperCase()}` : "AGENT-001"; const languageOptions = [ { value: "en", label: t("profile.languageOptionEnglish") }, { value: "am", label: t("profile.languageOptionAmharic") }, { value: "fr", label: t("profile.languageOptionFrench") }, { value: "ti", label: t("profile.languageOptionTigrinya") }, { value: "om", label: t("profile.languageOptionOromo") }, ]; const initialLetter = displayName?.trim().charAt(0).toUpperCase() || "U"; return ( {/* Profile Header */} {/* Avatar with + icon */} {profileImage ? ( ) : ( )} {/* Agent Info Card: Name, Role, Agent ID, Email */} {profileLoading ? "..." : displayName} Role: Agent Agent ID: {agentId} {profileLoading ? "..." : displayEmail} {/* Edit Profile Button */} Edit profile Inventories {/* Inventories Card - grouped items */} {/* Points */} Points {pointsTotal ?? 0} {/* Help and Support */} Help & Support {/* Terms and Conditions */} router.push(ROUTES.TERMS)} className="p-4 flex-row items-center justify-between" activeOpacity={0.7} > Terms & Conditions Information {/* Preferences Section */} Preferences {/* Preferences Card - grouped items */} {/* SMS notifications */} SMS notifications {/* Divider */} {/* In-app notifications */} In-app notifications {/* Divider */} {/* Email notifications */} Email notifications {/* Divider */} {/* Biometric Login (Face ID) */} Biometric Login {/* Divider */} {/* Language */} setLanguageSheetVisible(true)} className="p-4 flex-row items-center justify-between" activeOpacity={0.7} > {t("profile.languageLabel")} { languageOptions.find((opt) => opt.value === language) ?.label } {/* Divider */} {/* Reports / Transaction history */} View Reports {/* Divider */} {/* Change Password (placeholder) */} Change Password {/* Divider */} {/* PIN Code */} PIN Code {/* Logout - separate card */} Logout setLanguageSheetVisible(false)} maxHeightRatio={0.4} > {languageOptions.map((opt) => { const selected = language === opt.value; return ( { setLanguage(opt.value as any); setLanguageSheetVisible(false); }} className="py-3 flex-row items-center border-b border-gray-100" > {selected ? ( ) : ( )} {opt.label} ); })} ); }